Skip to content

Instantly share code, notes, and snippets.

@kxhitiz
Last active December 13, 2015 16:58
Show Gist options
  • Save kxhitiz/4944126 to your computer and use it in GitHub Desktop.
Save kxhitiz/4944126 to your computer and use it in GitHub Desktop.
Using Mongoid with Rspec
#Using Mongoid with Rspec
#To prep for Rspec, make your gemfile look like this (Assuming Rails 3.x):
gem 'rspec-rails', :group => [:test, :development]
group :test do
gem 'database_cleaner'
end
#Run: bundle install rails g rspec:install
#The first error message:
#undefined method `fixture_path=' for # (NoMethodError)
#Oops, just needed to comment out the line spec_helper.rb:
config.fixture_path = "#{::Rails.root}/spec/fixtures"
#Another error: undefined method `use_transactional_fixtures=' for # (NoMethodError)
#Oops, just needed to comment out the line spec_helper.rb:
config.use_transactional_fixtures = true
#Ideally, you want a clean database when you start each test. This is where the gem database_cleaner comes in handy.
#Then add this to spec_helper.rb file:
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
#That's it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment