Database Cleaner issue with Capybara webkit

北城余情 提交于 2019-12-03 02:51:07

Quick answer:

Configure your JavaScript tests to use truncation instead of transactions:

DatabaseCleaner.strategy = :truncation

Longer explanation:

The transaction strategy doesn't work well with JavaScript tests, because most JavaScript-capable capybara drivers run the tests in a different thread than the application code.

Here's a basic outline of the process:

  • Capybara boots up your rack application using webrick or thin in a background thread.
  • The main thread sets up the driver, providing the port the rack application is running on.
  • Your tests ask the driver to interact with the application, which causes the fake web browser to perform requests against your application.

This is necessary because it's difficult to make a fake browser that performs requests against an in-memory Rack application. In some database drivers, it isn't safe to perform queries from multiple threads against the same transaction.

The end result of this is that you need to commit transactions in your test code in order for the data to be visible in your application code. The easiest way to fix this is to use the truncation database cleaner strategy.

You can configure RSpec (or Cucumber) to use transactions for everything but JavaScript tests. This will be faster for non-JavaScript tests while still working for JavaScript tests.

Avdi Grimm has a good blog post on this subject that describes the solution in detail: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

Transaction based startegies do not work with cucumber. The reason for this is that you have two separate processes running, one is running you app server and then other one doing actual requests. There are different ways around this, but they are dirty hacks. The clean solution is to use truncation as you DatabaseCleaner strategy.

DatabaseCleaner.strategy = :truncation

Before do
  DatabaseCleaner.clean_with :truncation
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!