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.

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
end
end
end

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.