I'm Co-Founder & CTO of BlockAvenue. , connect with me, or subscribe.

Testing MongoDB with Mongoid, RSpec, and Rails

Whether using unit tests or rspec, you may find some support missing for testing your MongoDB-based application such as clearing the collections before test and waiting for updates to finish before proceeding. With a little Ruby both are easily supported.

Clearing collections before test

Each test will populate the collection(s) under test with the necessary documents so we need to clear those collections before the test to make our world sane again. Tests that fail from the leftovers of other tests are no fun.

While you could do this in a super class, I’ll add it to the RSpec config in spec_helper.rb:

Waiting for updates and conditions

If the operation under test is an update to the database, you could easily create race conditions:

With the addition of a wait function in spec_helper.rb:

We can now wait until the condition is true or fail after a short timeout if it is not: