Testing modules that get included in a controller

October 23, 2008 Pivotal Labs

We recently ran into a problem of testing a module that was getting included in multiple controllers. More specifically, we wanted to test a before_filter that the module was adding to each of the controller.

The code uses rspec for testing and we found out that there was no easy way for us to test the module.

The simplest way that would have worked would have been to test the controller that included the module. This was our first cut even though we were not happy with it at all since it was not testing the module in isolation. After asking around, we finally figured it out.

Here’s some code

module ShowPageModule
  def self.included(base)
    base.class_eval do
      before_filter :show_title_in_view

  def show_title_in_view
    #do something..set some instance variable
    @fancy_page = true

We want to test the show_title_in_view method.
The idea is to create a dummy controller and include the module so that you can test the method

So here’s how you do this in rspec
We define the controller, add some routing code and include it in test

class FakeController < ApplicationController
  include ShowPageModule
  def test
    head :ok

ActionController::Routing::Routes.add_route('fake_page/test', :controller => 'fake_page', :action => 'test')

describe 'ShowPageModules', ' included in a ' do
  describe FakeController do
    it "declares a before filter that sets the variable" do
      get :test
      assigns(:fancy_page).should be_true

It is that simple and you now have a very isolated unit test.
The trick was to figure out the routing code.

Some caveats

  • Since routes are singleton, be very careful while adding routes
  • Also try to name your fake controller uniquely so that there are no conflicts

About the Author


More Content by Pivotal Labs
Better array assertions using collect
Better array assertions using collect

You could do this: Person.tall_people.length.should == 3 Person.tall_people[0].should == people(:linda) Pe...

Effective Markdown Editing with the WMD editor and the Save Text Area Firefox plugin
Effective Markdown Editing with the WMD editor and the Save Text Area Firefox plugin

Anytime I need to edit Markdown, I reach for the WMD editor. Their splitscreen demo is the most effective w...

Enter curious. Exit smarter.

Register Now