Reputation: 238717
I am working on creating a new Gem that has some models which use Mongoid. I would like to test my gem using RSpec. I have started using RSpec for writing tests. I have installed a gem called mongoid-rspec and set it up according to its documentation.
Now I need to tell Mongoid how to configure itself. Running rspec from the command line, it's now telling me that it couldn't find a mongoid.yml file and that I should run rails g mongoid:config
. Obviously this won't work since I am testing a gem.
I'm new to rspec and mongoid, so I'm not quite sure of all the things I need to do to configure it properly in this test environment. It might be as easy as creating this mongoid.yml file, but I'm not sure of the best place to put it.
Any help would be appreciated. Thanks!
Upvotes: 2
Views: 3632
Reputation: 6720
Just add the following snippet to spec_helper
config section:
# Clean up all collections before each spec runs.
config.before do
Mongoid.purge!
end
This will clear mongo database before each test.
Also you can use factory_girl
(it works fine with Mongoid) and very helpful mongoid-rspec
gem: https://github.com/evansagge/mongoid-rspec
..and basically that's it ;)
Upvotes: 1
Reputation: 1942
Here is how you can make Mongoid work in the context of a gem rspec test.
Setup a Mongoid configuration file under spec/config/mongoid.yml
for example.
development:
sessions:
default:
database: your_gem_db
hosts:
- localhost:27017
in your spec/spec_helper.rb
file you can add
Mongoid.load!('./spec/config/mongoid.yml')
Please find further details about this issue on the Mongoid setup Documentation page
Upvotes: 3