Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 211
Alexey Avatar asked Mar 25 '11 14:03

Alexey


1 Answers

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

like image 88
Alexey Avatar answered Nov 15 '22 17:11

Alexey