我有一些功能需要在运行之前将大量数据加载到测试数据库中。我已将这些规格标记为“慢”。我想要完成的是任何标记为“慢”的特性规范将只填充数据库一次,将其用于该特性中的所有规范,然后在该特性完成后清理整个数据库。
以下是我设置 spec_helper.rb 的方法:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.after(:all, :slow => true) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
然后设置这些功能,如:
feature "Options page", :slow do
before(:each) do
DatabaseCleaner.strategy = :truncation, { except: %w[ products options models prices ] }
end
end
在 database_cleaner 1.3 之前,这对我来说都很好。现在,一些规范可以正常运行,但随后它们都开始失败:
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
Mysql2::Error: SAVEPOINT active_record_1 does not exist: ROLLBACK TO SAVEPOINT active_record_1
我应该在这里使用不同的策略吗?有人对我在这里要完成的工作有经验吗?