Setting up RSpec with the new Rails / MongoID application

I am starting a new application and notice the missing documentation the last time I created a MongoID application from scratch. Namely, they suggested on a page that no longer exists ( http://mongoid.org/docs/integration/ ) to include some code to remove MongoID collections (after tests).

It is no longer mentioned on the site ... is this (**** below) no longer considered necessary or good practice?!?

#spec/spec_helper.rb: ... RSpec.configure do |config| config.mock_with :rspec # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures #config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. #config.use_transactional_fixtures = true # Below from <http://mongoid.org/docs/integration/> **** config.after :suite do Mongoid.master.collections.select do |collection| collection.name !~ /system/ end.each(&:drop) end end 
+8
ruby-on-rails mongoid rspec
source share
3 answers

Modify the spec / spec_helper.rb file to add this:

 RSpec.configure do | config |
   # Other things

   # Clean up the database
   require 'database_cleaner'
   config.before (: suite) do
     DatabaseCleaner.strategy =: truncation
     DatabaseCleaner.orm = "mongoid"
   end

   config.before (: each) do
     DatabaseCleaner.clean
   end
 end
+11
source share

It also seems to work with Rails3 and tidier

 config.before :each do Mongoid.purge! end 

He does not need an extra GEM.

+12
source share

You can continue to do (though perhaps switch to the previous set) that the DatabaseCleaner pearl is good, though.

  config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end 
+2
source share

All Articles