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

血红的双手。 提交于 2019-12-31 12:59:14

问题


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

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