---
title: Testing a before_filter
teaser: How to set up test suite for verifying a before_filter in Rails.
tags: web,rails,testing
author: Adarsh Pandit
published_on: 2012-04-06
---

Recently, we implemented a feature that required a before_filter in
`ApplicationController` and whitelisting some other controllers using
`skip_before_filter`.

We couldn't actually test something like this directly because callbacks aren’t
really methods. They’re entirely used for their side effects so we can only test
what happens when we invoke one. Let's check out an example:

```ruby
require 'test_helper'

class ApplicationControllerTest < ActionController::TestCase
  context 'ensure_manually_set_password' do
    setup do
      class ::TestingController < ApplicationController
        def hello
          render :nothing => true
        end
      end

      ActionController::Routing::Routes.draw do |map|
        map.hello '', :controller => 'testing', :action => 'hello'
      end
    end

    teardown do
      Object.send(:remove_const, :TestingController)
    end

    context 'when user is logged in' do
      setup do
        @controller = TestingController.new
      end

      context 'and user has not manually set their password' do
        setup do
          @user = Factory(:user, :manually_set_password => false)
          login_as @user
          get :hello
        end

        should 'redirect user to set their password' do
          assert_redirected_to new_password_path(@user.password_token)
        end
      end
    end
  end
end
```

Note the use of the double-colon prepended to the TestingController, which
ensures the class is top-level, not an inner class of ApplicationControllerTest.
That way we can just do `:controller => 'testing'` and not have to write
`:controller => 'application_controller_test/testing'`. We also use the private
method `remove_const` to remove the class after we're done, so we don't litter
the namespace.

[Co-written with Gabe Berke-Williams](http://thoughtbot.com/about/#gberkewilliams)
