Displaying articles with tag

Testing an abstract controller (like ApplicationController)

Posted by joakimk, Thu Jan 28 15:28:00 UTC 2010

This is one way to test before_filters in your ApplicationController or other abstract controllers. Make sure you don’t miss the “after” part!

One thing to keep in mind though is that re-loading the routes can take some serious time (6 seconds in the app Im working on). I’d like to find a better way of testing this.

require File.dirname(__FILE__) + '/../../spec_helper'

describe ApplicationController do

  class TestController < ApplicationController
    def index
    end
  end

  controller_name :test

  before :each do
    ActionController::Routing::Routes.draw do |map| 
      map.resources :test
    end
  end

  after :all do
    load(RAILS_ROOT + "/config/routes.rb")
  end

  it "should do something" do
    get :index
    # assert stuff
  end

end

1 comment | Filed Under: | Tags:

Testing filter_parameter_logging using RSpec

Posted by joakimk, Wed Oct 29 17:27:00 UTC 2008

One way of speccing parameter filtering.

Controller:
    filter_parameter_logging :password
Spec:
    it "should filter password" do
      before_filter, after_filter = {}, {}
      before_filter['user'] = { 'username'=>'foo', 'password'=>'bar' }
      after_filter['user'] = { 'username'=>'foo', 'password'=>'[FILTERED]' }      
      controller.filter_parameters(before_filter).should == after_filter
    end

0 comments | Filed Under: | Tags:

RSpec controller specs gotcha...

Posted by joakimk, Sun Jun 29 14:31:00 UTC 2008

Another little tip. If you’re using an IP-based check for access (for example an before_filter in application.rb), the controller specs will fail (silently) to do anything for the ‘get’, ‘put’, ‘update’, ... unless you include ‘0.0.0.0’ as a valid ip.

0 comments | Filed Under: | Tags: