1

rspec+capybara を使用していくつかのテストを作成しました。テストコード

rspecでそれらを実行すると、すべて合格します

git:(master) ✗ rspec    
Rack::File headers parameter replaces cache_control after Rack 1.5.
WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.

Finished in 32.54 seconds
8 examples, 0 failures

しかし、ガードを使用すると、一部が失敗する傾向があります(一部のテストは失敗する場合があり、時々失敗する場合があります)
ガード出力

この動作はどのように説明できますか? そして、それはどのように修正できますか?

更新 1
私はすでにgem 'database_cleaner'この構成で使用しています:

config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:transaction)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = :transaction
  config.use_transactional_examples = true

更新 2
いくつかのファイルを変更しましたhttps://github.com/Asmmund/notes/commit/a5e0a43d6247bb8f937fb7e9dcc8d8cfa7bfc4ea

4

1 に答える 1

1

テストの作成方法に応じて、Capybara は Selenium ドライバーを使用してテストを実行します。Selenium はトランザクション フィクスチャと互換性がないため、それを false に設定する必要があります。

一般に、Capybara (またはほとんどの統合テスト) で Fixtures を使用しないようにする必要があります。優れた統合テストでは、フィクスチャへの依存を最小限に抑え、代わりにエンド ツー エンド フロー中にデータを作成する必要があります (たとえば、ユーザー アカウントはシミュレートされたアプリ内アクションによって作成する必要があります。つまり、テストでは Capybara を使用してサインアップ フォームを送信する必要があります)。 )。ただし、DB にデータをシードする必要がある場合など、奇妙なケースは避けられません。多くの人が FactoryGirl などのファクトリを使用して成功しています。

テストがセレンの場合、DB のクリーンアップでは、トランザクションではなく切り捨て戦略も使用する必要があります。

詳細なガイドとして、このチュートリアルをチェックしてください。

注:あなたの場合、時間を節約するために、下から上に読んでください:

http://asciicasts.com/episodes/257-request-specs-and-capybara

于 2013-03-22T19:06:19.283 に答える