开发者

Capybara+Selemium: How to initialize database in an integration test code and make it visible in Rails application?

开发者 https://www.devze.com 2023-02-19 13:31 出处:网络
Configuration: Integration tests for Rails project using RSpec, Capybara, Selemium driver, SQLite database.

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_transa开发者_如何学Cctional_fixtures = false 
DatabaseCleaner.strategy = :truncation


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消