Ruby on rails 水豚失败后场景输出屏幕截图

Ruby on rails 水豚失败后场景输出屏幕截图,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我想在失败的场景后输出页面的屏幕截图 我正在使用水豚、rspec和launchy——我的gemfile group :development, :test do gem 'rspec-rails' gem 'capybara' gem 'launchy' end 我看到了各种建议,其中包括以下代码 After do |scenario| save_and_open_page if scenario.failed? end 因此,我创建了一个spec\suppor

我想在失败的场景后输出页面的屏幕截图

我正在使用水豚、rspec和launchy——我的gemfile

group :development, :test do
    gem 'rspec-rails'
    gem 'capybara'
    gem 'launchy'
end 
我看到了各种建议,其中包括以下代码

After do |scenario|
  save_and_open_page if scenario.failed?
end
因此,我创建了一个spec\support\env.rb文件,我将此代码放入其中(仅此而已,该文件中没有其他内容)。现在当我跑的时候

bundle exec rspec
我明白了

C:/Working/x/spec/support/env.rb:1:in `<top (required)>': undefined method `After' for main:Object (NoMethodError)
C:/Working/x/spec/support/env.rb:1:in`':main:Object(NoMethodError)的未定义方法'After'
如果我注释掉env.rb中的行,那么测试将按预期运行

我错过了什么

谢谢

版本:

  • 轨道4
  • rspec 2.14.1
  • 水豚2.1.0
  • launchy 2.3.0

尝试将其包装在Rspec.configure块中,类似于:

RSpec.configure do |config|
  config.after { |example_group| save_and_open_page if example_group.example.exception }
end

有一个gem,您只需将其添加到您的gem文件:

gem 'capybara-screenshot', :group => :test
所有说明都可以在这里找到:-

@ejosafat-extra“示例”。在您的回答中,我使用它如下:

RSpec.configure do |config|
  config.after { |example_group| save_and_open_page if example_group.exception }
end

我将其更改为:
RSpec.configure do | config | config.after{scenario | save|u and(u open|u page if scenario.failed?}end
以避开错误
“未定义的局部变量或方法‘scenario’”
但现在我得到了
未定义的方法失败?
很抱歉,它出现了一个输入错误,没有尝试解决方案。似乎after钩子生成了一个ExampleGroup,而不是块的一个示例,我编辑了我的答案。请让我知道它是否对你有用。这就是@ejosafat的分类。实际上,我最终使用了save_页面,并使用example_group.example.description来命名页面,这样就很容易看到哪个场景失败了。谢谢你的更新。