why does mongodb fail inconsistently with rails3 and rspec?
We have a rails 3, mong开发者_开发百科odb app using rspec, very normal setup I think in spec_helper:
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
config.before(:each) do
DatabaseCleaner.clean
end
But for some reason mongo seems to just fail at weird times in the tests inconsistently. That is, 50% of the time the spec will run and no failures. But 50% of the time a simple query to mongo will return a nil object and cause the spec to fail. Any idea where to start debugging? This never happens in development mode with a browser using the app, mongo is rock solid there. Could it be the test env?
Try this:
DatabaseCleaner.strategy = :truncation
Databasecleaner.orm = "mongoid"
config.before(:each) do
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
You could do it without DatabaseCleaner with:
config.before :each do
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
精彩评论