[rspec-users] Spec'ing controller macros

Pat Maddox pergesu at gmail.com
Tue Apr 29 13:12:26 EDT 2008

On Tue, Apr 29, 2008 at 9:48 AM, Matt McNeil <nabble.108 at xoxy.net> wrote:
>  describe ApplicationController do
>   class FooController < ApplicationController
>     def index; render :text => "foo"; end
>   end
>   controller_name :foo
>   it "should turn off session management for requests made by robot user
>  agent" do
>     request.stub!(:user_agent).and_return("Google Robot")
>     controller.should_receive(:session).with(:off)
>     get :index
>   end
>  end

Hi Matt,

The problem here is that it's actually the controller class that's
receiving the 'session' message, not a controller object.

In this case, mocking the call isn't going to do you much good.  The
:if block wouldn't get evaluated, forcing you to just duplicate the
method arguments.

The good news is you can test the desired behavior with a more
state-based approach:

  it "should turn the session off" do
    request.stub!(:user_agent).and_return("Google Robot")
    get :index
    controller.should_not be_session_enabled


More information about the rspec-users mailing list