RSpec/Capybara/JS 测试单独通过,但当 运行 一起时失败

RSpec/Capybara/JS tests pass in isolation, but fail when run together

我的问题似乎非常类似于:rspec test passes in isolation, but fails when run with other tests - but the accepted answer is something I'm already doing: I'm using avdi's database cleaner setup (and I've tried deletion instead of truncation like thoughtbot uses in their suspenders example app): http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

所以这个代码的账户记录:

let(:account) { create(:account) }

似乎正在被删除。我正在访问一个未找到的页面,因为该帐户不存在。因此,在不同的数据库清理器策略之间来回切换似乎是导致问题的原因?

我可以从字面上 运行 rspec --only-failures 并且我刚刚失败的所有测试都会通过。它只发生在 feature/capybara 规格下。

编辑:这是我的配置:https://gist.github.com/gregblass/b886f79b0d8e2e2015af

更新:以下是一些失败测试的示例:https://gist.github.com/gregblass/1b878d92a2b9dad355e0

更新 2:它发生在我进行 js: true 测试之后。这些东西搞砸了我随后的其他非 js 水豚测试。

我能够使用 rspec --bisect 并确定罪魁祸首 - 它们是 JS 水豚测试。在我 运行 JS 测试之后,似乎每个人都推荐的数据库清洁器切换策略是删除我在随后的非 js 水豚测试中声明的帐户。

这是一种解决方法:

http://stefan.magnuson.co/articles/rails/robust-integration-testing-in-rails-4-with-rspec-capybara-and-selenium/

Many tutorials and Whosebug answers out there have JS and non-JS tests mixed together, with the strategy being switched between transaction and truncation on each test.

In my experience this has resulted in database access race conditions that have caused otherwise well-written, independent tests to fail intermittently.

The solution I have settled on is to run all non-JS tests first (shuffled), and then all JS tests (also shuffled). This allows for discovery of tests that incorrectly expect certain state, or fail to clean up after themselves (by virtue of the random execution order), while not attempting to freely mix JS and non-JS tests. As these different classes of test have different purposes, I see no disadvantage in this approach, with the exception of it being somewhat non-standard.

另一种方法是将您的数据库清理器配置更改为以下内容(取自@ryanbigg 的多租户与 rails 一书):

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

config.around(:each) do |example|
  DatabaseCleaner.cleaning do
    example.run
  end
end

速度较慢,但​​在我的 Macbook Pro 16GB/SSD 上我什至看不出有什么区别。

感谢@tomwalpole 提供的所有帮助,感谢@barelyknown 建议 rspec --bisect,感谢 @ryanbigg 提供的关于使用水豚测试多租户的好书以及有效的代码示例,感谢 Stefan Magnuson文章(我找不到 SO 句柄)。