清理测试数据库,仅生成的数据 运行 使用 RSPEC 和 Capybara 的测试
Clean test database, only of the data generated running the tests with RSPEC and Capybara
我在 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
你可以用这个
rake db:reset RAILS_ENV=test
如果您在 table 中创建包含您希望在测试中保存的数据的记录,则没有简单的方法(database_cleaner 的简单配置更改等)你想要什么 - 维护测试开始时存在的数据。那是因为那将是 database_cleaner 的事务模式,它与 rails 4.1 中的 Capybara 和 JS 测试不兼容(在 Rails 5.1 中事务模式是兼容的,而 database_cleaner 是大多数配置不再需要)
如果您想要从测试中保存的 table 中没有 creating/deleting/updating 记录,那么您可以告诉 database_cleaner 跳过那些 table。
既然做你想做的事并不容易,你应该问问自己为什么你正在尝试做大多数其他人不会做的事情,也许可以调整你做事的方式。
如果您仍想继续尝试做的事情(并且您没有修改测试中的任何现有数据(只是添加新行),那么您需要创建一个 before 挂钩在每个 table 中存储最大 id,然后是一个删除具有较大 id 的记录的后挂钩,并且不使用 database_cleaner
我在 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
你可以用这个
rake db:reset RAILS_ENV=test
如果您在 table 中创建包含您希望在测试中保存的数据的记录,则没有简单的方法(database_cleaner 的简单配置更改等)你想要什么 - 维护测试开始时存在的数据。那是因为那将是 database_cleaner 的事务模式,它与 rails 4.1 中的 Capybara 和 JS 测试不兼容(在 Rails 5.1 中事务模式是兼容的,而 database_cleaner 是大多数配置不再需要)
如果您想要从测试中保存的 table 中没有 creating/deleting/updating 记录,那么您可以告诉 database_cleaner 跳过那些 table。
既然做你想做的事并不容易,你应该问问自己为什么你正在尝试做大多数其他人不会做的事情,也许可以调整你做事的方式。
如果您仍想继续尝试做的事情(并且您没有修改测试中的任何现有数据(只是添加新行),那么您需要创建一个 before 挂钩在每个 table 中存储最大 id,然后是一个删除具有较大 id 的记录的后挂钩,并且不使用 database_cleaner