我真的不明白为什么
DatabaseCleaner不清理我的测试数据库.这是我得到提示
1.9.2p290 :007 > DatabaseCleaner.clean
–
=> [#<DatabaseCleaner::Base:0x007fa7e4dd8b58 @autodetected=true,@orm=:active_record,@strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x007fa7e4dc14f8 @db=:default>>]
这似乎没有正确设置数据库(应该是:测试),所以我得到一个解决方案
DatabaseCleaner[:active_record,:connection => :test].clean # => nil
宝石似乎设置正确:
1.9.2p290 :007 > DatabaseCleaner[:active_record,:connection => :test] #<DatabaseCleaner::Base:0x007fe8fcfd4868 @orm=:active_record,@strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x007fe8fcfd2748 @db=:test,@connection_hash={"adapter"=>"sqlite3","database"=>"db/test.sqlite3","pool"=>5,"timeout"=>5000}>,@db=:test>
这似乎正确地设置了测试数据库,但是它仍然不能正确清理数据库.有什么建议么?
非常感谢你.
解决方法
即使数据库清理器配置正确,也很容易将数据留下.
config.before(:suite) do DatabaseCleaner.clean_with :truncation # clean DB of any leftover data DatabaseCleaner.strategy = :transaction # rollback transactions between each test Rails.application.load_seed # (optional) seed DB end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end
以上配置启动&在每个测试的任一侧运行清洁.
如果您之前使用过:所有在您的规格中,您最终可能会遇到数据:
describe User do # Before all is outside the before :each before :all do @user = User.create(:email => 'hello@example.com') end ...tests here end