如何在任何规范失败时自动生成Rspec save_and_open_page

ste*_*ble 13 rspec capybara

我有...

/spec/spec_helper.rb:

require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/dsl'

RSpec.configure do |config|
  config.fail_fast = true
  config.use_instantiated_fixtures = false 
  config.include(Capybara, :type => :integration)
end
Run Code Online (Sandbox Code Playgroud)

因此,只要任何规范失败,Rspec就会退出并向您显示错误.

在这一点上,我希望Rspec也自动调用Capybara的save_and_open_page方法.我怎样才能做到这一点?

Capybara-Screenshot看起来很有前途,但是虽然它将HTML和截图保存为图像文件(我不需要),但它不会自动打开它们.

lua*_*sus 13

在rspec的配置中,您可以为每个示例定义一个挂钩(https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks).它没有很好的记录,但这个钩子的块可以采取一个example参数.在example您可以测试的对象上:

  • 它是一个功能规格: example.metadata[:type] == :feature
  • 它失败了: example.exception.present?

剪切的完整代码应如下所示:

  # RSpec 2
  RSpec.configure do |config|
    config.after do
      if example.metadata[:type] == :feature and example.exception.present?
        save_and_open_page
      end
    end
  end

  # RSpec 3
  RSpec.configure do |config|
    config.after do |example|
      if example.metadata[:type] == :feature and example.exception.present?
        save_and_open_page
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)