我有......
/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
因此,只要任何规范失败,Rspec就会退出并向您显示错误。
此时,我希望Rspec也自动调用Capybara的save_and_open_page
方法。我怎么能这样做?
Capybara-Screenshot看起来很有希望,但是虽然它将HTML和截图保存为图像文件(我不需要),但它不会自动打开它们。
答案 0 :(得分: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
答案 1 :(得分:1)
在RSpec 2中结合Rails 4,我使用这个配置块:
# In spec/spec_helper.rb or spec/support/name_it_as_you_wish.rb
#
# Automatically save and open the page
# whenever an expectation is not met in a features spec
RSpec.configure do |config|
config.after(:each) do
if example.metadata[:type] == :feature and example.exception.present?
save_and_open_page
end
end
end