Patrick Ward words, code, and music

Using NullDB with MiniTest:Spec and Rails 3.2

I’ve been using MiniTest:Spec for testing my latest rails project. In addition, I’ve been trying to adapt the patterns and lessons discussed in Avdi Grimm’s Objects on Rails (I recommend purchasing his book, I did and was not dissapointed).

However, Rails 3.2+ does not work well with NullDB. It would crash when running the NullDB::nullify and NullDB::restore commands. After a few trials, I found that I could bypass thie built-in nullify command and establish the ActiveRecord connection myself.

First, I’m using the latest version from the GitHub repository in my Gemfile.

Gemfile - using the GitHub repository for the latest version:

gem 'activerecord-nulldb-adapter', :git => 'git://'

Next, I created spec helpers to setup and teardown the NullDB connections. This file loads in another spec_helper_lite.rb file (discussed in the Objects on Rails book). I then require the spec_helper_nulldb.rb file in those tests that can use the NullDB adpater in place of the regular database adapter that I’m using.


require "bundler/setup"
require_relative 'spec_helper_lite'

require 'active_record'

module SpecHelpers
  def setup_nulldb
    require 'nulldb/rails'
    schema_path = File.expand_path('../db/schema.rb', File.dirname(__FILE__))
    # NullDB.nullify(:adapter => :nulldb, :schema => schema_path)
    ActiveRecord::Base.establish_connection(:adapter => :nulldb, :schema => schema_path)

  def teardown_nulldb
    # NullDB.restore
    spec_path = File.expand_path('../config/database.yml', File.dirname(__FILE__))
    spec = YAML.load_file(spec_path)

So far, this is working great and gives me the benefits of NullDB’s speed.