[rspec-users] Spec the Application Controller application.rb

Pat Maddox pergesu at gmail.com
Mon Apr 21 17:06:25 EDT 2008

On Mon, Apr 21, 2008 at 8:27 AM, Andy Croll <lists at ruby-forum.com> wrote:
> How would I go about writing specs for methods in the Application
>  Controller:
>  I'm thinking of simple methods that do authentication, through before
>  filters or for example how might I spec this method in an
>  application_spec.rb?
>   def store_location
>     session[:return_to] = request.request_uri
>   end
>  The trouble is I'm not generating a request object as the methods are to
>  be used by all controllers... I've tried stubbing...
>  request = mock_model(URI::HTTP, :request_uri => "something")
>  ...or setting the variable in the spec itself...
>  request.response_uri = "something"
>  ...but I can't seem to get the approach right!

Hey Andy,

You can define a controller method on the fly in order to test this
out.  I just did a quick experiment to demonstrate it...obviously
modify to suit your needs.

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

describe "a before_filter" do
  class FooController < ApplicationController
    def index; render :text => "foos"; end
  controller_name :foo

  it "should work" do
    get :index
    assigns[:assigned].should_not be_blank

class ApplicationController < ActionController::Base
  before_filter :set_assigned

  def set_assigned
    @assigned = "yay"

More information about the rspec-users mailing list