Test a gem with the Rails 3 stack

My previous post covers testing a gem that makes some change to how ActiveRecord works. But what if you want to test a gem that supplies some new behavior to another part of rails, like say ActionController. How do you properly setup that environment without including an entire, mostly blank, rails application into your gem’s sources?

The answer is to essentially do what would normally be done if you were testing inside of a Rails application, by looking at what files get required by your testing framework. I use rspec, and here is my spec_helper.rb.

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

ENV["RAILS_ENV"] ||= 'test'

require 'rubygems'
gem 'actionpack', '>= 3.0.0'
gem 'activesupport', '>= 3.0.0'
gem 'activemodel', '>= 3.0.0'
gem 'railties', '>= 3.0.0'

# Only the parts of rails we want to use
# if you want everything, use "rails/all"
require "action_controller/railtie"
require "rails/test_unit/railtie"

root = File.expand_path(File.dirname(__FILE__))

# Define the application and configuration
module Config
  class Application < ::Rails::Application
    # configuration here if needed
    config.active_support.deprecation = :stderr 

# Initialize the application

require 'rspec/rails'

RSpec.configure do |config|

require 'my_gem'

Due to Rails 3 modularity, we can now include only the parts of it we need for our tests. So instead of requiring “rails/all” you can see me bringing in only action_controller and some test unit helpers.

I banged my head against another problem, which is not visible from the code here. Rails looks for its root directory by looking for a specific file starting with the directory of the file in which you’ve done this configuration. I’ve seen evidence that some code looks for script/rails and other code looks for config.ru. It isn’t necessary for either of these files to have anything in them, but simply to exist as you’d expect in a rails application. I put them in my ‘spec’ directory.

  • spec/config.ru
  • spec/script/rails

I’m testing a gem that does something with controllers, so I also setup spec/config/routes.rb. All of my tests use the same two controllers, so I set those up in spec/app/controllers/.

After all this setup, spec tests work as usual in a Rails application for spec/controllers, spec/routing, etc.

This entry was posted in rails and tagged , . Bookmark the permalink.

One Response to Test a gem with the Rails 3 stack

  1. Thanks a lot!

    It’s really worthy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s