【发布时间】:2017-12-18 21:39:53
【问题描述】:
我在 rails 4.1 中有一个应用程序,我正在使用 RSPEC 和 Capybara 对其进行测试,对于她我有一个测试数据库,其中包含用于执行测试的数据,我正在运行生成一些存储在 bd 中的数据的测试,或者我需要的是在运行测试后这些数据不存储在其中。
我使用了 gem database_cleaner(干净的测试数据库,仅使用 RSPEC 和 Capybara 运行测试生成的数据),但这会清除测试 bd 的所有数据,我只想擦除由测试。
我有这个配置,测试是javascrpit测试:
config.use_transactional_fixtures = false
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
DatabaseCleaner.strategy = :truncation
end
end
【问题讨论】:
-
很难理解为什么您不希望数据库在您运行的每个测试中都处于干净状态 - 在测试数据库中保存数据的目的是什么 - 您能否澄清一下您的使用案例?
标签: ruby-on-rails ruby rspec capybara rspec-rails