RSpec, Mongo and Database Cleaner

This is kinda obvious, once you see it… But I’d figure it might help someone, someday.

I wanted to create a document one time, so I put it in the before :all block.

Yet, in the “it should” block, the document was gone, spec failed.

If I changed to a before :each block, the spec passed

So I changed the spec_helper from doing a clean for each, to using truncation. I also switched to doing the clean to the before :suite block (so that data didn’t build up in Mongo):

  #DatabaseCleaner[:mongo_mapper].strategy = :truncation

config.before(:each) do
  DatabaseCleaner[:mongo_mapper].strategy = :truncation

And now things are as I expected them to be when using a before :all block…

I can repeatedly run the specs, and they pass.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.