13

私は持っています...

/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は終了し、エラーを表示します。

save_and_open_pageその時点で、RspecにもCapybaraのメソッドを自動的に呼び出してもらいたいと思います。これどうやってするの?

Capybara-Screenshotは有望に見えますが、HTMLとスクリーンショットの両方を画像ファイル(私は必要ありません)として保存しますが、それらを自動的に開くことはありません。

4

2 に答える 2

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
于 2013-06-05T09:06:35.640 に答える
1

Rails4と組み合わせたRSpec2では、次の構成ブロックを使用します。

# 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
于 2014-06-24T21:20:58.533 に答える