Last active
December 13, 2015 16:58
-
-
Save kxhitiz/4944126 to your computer and use it in GitHub Desktop.
Using Mongoid with Rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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