Getting ‘rake test’ Running with Rails 3 and MongoDB

I’ve recently started a re-write of my Addressbook application. Addressbook was my first Rails application, as well as my first full fledged Ruby application…and boy does it show! So, trust me, the re-write is justified :)

I saw the re-write as a good opportunity to get more familiar with Rails 3 and MongoDB. After getting Rails 3 setup to use MongoDB via MongoMapper (Ben Scofield’s setup script gets you most of the way there), I quickly realized that the tests would not run.

jwood-mbp:addressbook jwood$ rake test
(in /Users/jwood/dev/personal/addressbook)
Errors running test:units, test:functionals, test:integration!

jwood-mbp:addressbook jwood$ rake test:units
(in /Users/jwood/dev/personal/addressbook)
rake aborted!
Don't know how to build task 'db:test:prepare'

Some Googling revealed this post by Yehuda Katz, in which he says:

Additionally, users can completely remove the ActiveRecord gem, and be rid of the generators, rake tasks and other peripheral elements. An example: if DataMapper implements a db:test:prepare rake task, a Rails developer can replace ActiveRecord with DataMapper, and the test:units rake task will use DataMapper’s new tasks.

We’re not using DataMapper here, but the same thing applies to MongoMapper. So, all that is needed is to implement a db:test:prepare task, which I did in lib/tasks/mongo.rake

namespace :db do
  namespace :test do
    task :prepare do
      # Stub out for MongoDB

As of right now, the task does nothing. I may end up adding some functionality to it as I work on the project. We’ll see.

Running the tests again, I can see that was all that was needed to get going.

jwood-mbp:addressbook jwood$ rake test:units
(in /Users/jwood/dev/personal/addressbook)
Loaded suite /Users/jwood/.rvm/gems/ruby-1.9.1-p378@rails3/gems/
Finished in 0.062854 seconds.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips