Using Mongoid with Rspec

I’m a fan of testing. I’m still trying to discipline myself to test before I write code, that’s been a tough habit to develop, however testing after I develop a feature is a good habit that has paid dividends. Lately I’ve started learning how to use Rspec for Rails.

Out of the box, Rspec is configured to use ActiveRecord. I’ve pretty much stopped using relational databases in favor of NoSql solutions. My favorite NoSql DB is MongoDB. The Ruby MongoDB ORM that I’ve been using is Mongoid.

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

Then run:

bundle install
rails g rspec:install

When I started learning how to use Rspec, I started having problems. The first was the following 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"

Was I safe yet? Nope. 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

I should have known as the there are comments in the file.

Now, when I run my specs, the changes to the database persist from one test to the next. 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 your 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!

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

If you made it this far, read my blog on software entrepreneurship and follow me on Twitter: @jprichardson.

Installing MongoDB 1.8.1 on Ubuntu 10.04 LTS

A lot of this information will seem obvious to a lot of you. But it did require me to perform a few Google searches, so hopefully I can save you the trouble.

I provide a script to automate this process:

This script is deprecated now. Fortunately 10Gen (the company who makes MongoDB) provides an Apt repository. Do not use the current MongoDB version in the Ubuntu apt sources. It’s version 1.6.*

All you need to do:

vim /etc/apt/sources.list

Add the following line:
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen


apt-get update
apt-get install mongodb-10gen

By default MongoDB runs in trusted mode. This means that it’s listening on port 27017 for any connection. We want it to only listen on localhost.

Edit the file: /etc/mongodb.conf

Add the following lines:
bind_ip 127.0.0.1
noauth = true ##### YOU MUST add this line if you use bind_ip

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

That’s it. Follow me on Twitter: @jprichardson and read my blog on software entrepreneurship.

-JP

Mongoid UTC Times

By default Mongoid will store your Time objects in your local timezone. It’s not made clear on the installation page, but all you have to do is add “use_utc: true” to the defaults in your mongoid.yml configuration file. Yes Virginia, it’s that simple.

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

-JP

Follow

Get every new post delivered to your Inbox.