Rails 3, RSpec, Mongoid and Database Cleaner

Posted by – July 5, 2010

After wrestling with various combinations of cleaning out my database between tests, this is what I'm using on a new Rails 3 application that uses Mongoid, RSpec 2, and Database Cleaner. I have one table (neighborhoods) which is populated using rake db:seed, so I'm excluding that from the cleanup.

Put this into your spec/spec_helper.rb:

require 'database_cleaner'
 
RSpec.configure do |config|
  config.mock_with :rspec
 
  config.before(:each) do
    DatabaseCleaner.orm = "mongoid" 
    DatabaseCleaner.strategy = :truncation, {:except => %w[ neighborhoods ]}
    DatabaseCleaner.clean
  end
end

UPDATE: This isn't working for me now. Apparently the config.before(:each) part isn't being called in the versions of rspec (2.0.0.beta.21), cucumber (0.8.5), and cucumber-rails (0.3.2) that I'm using now. I'm now using the approach by Kevin Faustino here.

2 Comments on Rails 3, RSpec, Mongoid and Database Cleaner

Respond

  1. Louis Rose says:

    Thanks very much! This is exactly what I was after, and you saved me some time wrestling with these gems. Cheers!

  2. Rafael Lima says:

    If you do not use seed, you can try the code below

    config.before(:each) do
    Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
    end

Respond

Comments

Comments