Capybara+Selemium:如何在集成测试代码中初始化数据库并在Rails应用程序中可见?[英] Capybara+Selemium: How to initialize database in an integration test code and make it visible in Rails application?

本文是小编为大家收集整理的关于Capybara+Selemium:如何在集成测试代码中初始化数据库并在Rails应用程序中可见?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

配置: 使用RSPEC,Capybara,Selemium驱动程序,SQLITE数据库的Rails项目集成测试.

情况: 我与Capybara和默认的Rack_test驱动程序几乎没有集成测试.他们直接在数据库中创建用户注册(用于设计GEM).然后,他们像用户一样使用Capybara DSL登录并测试场景.

问题: 我试图将驱动程序更改为硒来测试JavaScript代码.现在测试失败,因为应用程序没有看到测试创建的用户注册.

调查: 看起来Selenium驱动程序在过渡方面的工作方式有所不同,因此在Web应用程序中,测试中进行的更改是不可见的.可能的解决方案涉及:

config.use_transactional_fixtures = false 
DatabaseCleaner.strategy = :truncation

推荐答案

对我而言,从这里 .dzone.com/posts/show/13057" rel =" noreferrer">在这里:

`

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

`

虽然对我的Contoller&Model Specs执行时间造成了小惩罚(从40到43秒).

本文地址:https://www.itbaoku.cn/post/597652.html

问题描述

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

推荐答案

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