在使用 RSpec 测试我的模型时,我使用以下代码来清理我的数据库:
config.before(:suite) do
begin
DatabaseCleaner.start
ensure
DatabaseCleaner.clean
end
end
config.after(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
我所有的模型测试都通过了,但是当我测试我的控制器时,它们似乎使用了与我的模型操作相同的数据集,从而使我的测试出错。
我可以通过使用以下代码来通过我的控制器测试:
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
但是,这会导致我的模型测试失败。关于如何在不破坏一组测试或另一组测试的情况下组合这些块的任何建议?