Configuring RSpec with new Rails/MongoID application
Asked Answered
B

4

8

I'm starting a new app and notice some missing documentation from the last time I built a MongoID app from scratch. Namely they used to suggest on a page that no longer exists (http://mongoid.org/docs/integration/) to include some code to drop MongoID's collections (after tests).

It's not mentioned on site anymore...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
Burchfield answered 28/4, 2011 at 22:13 Comment(0)
N
11

Modify the file spec/spec_helper.rb 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
Noyade answered 28/4, 2011 at 22:49 Comment(1)
Doesn't work with Mongoid 4.0 and rspec-core 2.14.7. I'm using @Irena solution belowIncur
I
13

This also seems to be working on Rails3 and is more neat

config.before :each do
  Mongoid.purge!
end

It does not need an additional GEM.

Irena answered 7/9, 2012 at 8:33 Comment(2)
As soon as someone verifies this (I don't have an active MongoID project to try this on at the moment) I'll move the checkmark here as this does seem much simpler.Burchfield
It works, but with one caveat: Mongoid.purge! will also remove all indexes. This means that any specs relying on an index with unique:true will fail. The workaround is to call create_indexes on each of your model classes right after the call to purge.Setzer
N
11

Modify the file spec/spec_helper.rb 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
Noyade answered 28/4, 2011 at 22:49 Comment(1)
Doesn't work with Mongoid 4.0 and rspec-core 2.14.7. I'm using @Irena solution belowIncur
K
2

You can do keep doing (although maybe switch to before suite) that -- the DatabaseCleaner gem is nice 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
Kalmar answered 28/4, 2011 at 22:27 Comment(0)
L
0

The gist of database cleaner for mongo(id)

  config.before(:each) do
    ::Mongoid::Clients.default.collections.each do |collection|
      collection.delete_many
    end
  end
Lauraine answered 24/4, 2024 at 11:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.