问题
Configuration: Integration tests for Rails project using RSpec, Capybara, Selemium driver, SQLite database.
Situation: I had few integration tests with Capybara and default rack_test driver. They create a user registration (for Devise gem) directly in database. Then they login and test a scenario using Capybara DSL like a user would do.
Problem: I tried to change a driver to Selenium to test JavaScript code as well. Now the tests fail because application does not see a user registration that the tests created.
Investigation: It looks like Selenium driver works differently with transations, so changes made in a test are invisible in the web application. Possible solution make involve:
config.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
回答1:
For me worked solution from here and here:
- add to Gemfile
gem database_cleaner
- create file
spec/support/javascript.rb
with content
`
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
if example.metadata[:js]
Capybara.current_driver = :selenium
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start
end
end
config.after(:each) do
Capybara.use_default_driver if example.metadata[:js]
DatabaseCleaner.clean
end
end
`
though it caused small penalti to my contoller & model specs execution time (from 40 to 43 seconds).
来源:https://stackoverflow.com/questions/5433690/capybaraselemium-how-to-initialize-database-in-an-integration-test-code-and-ma