Spec'ing controller macros

Hi,

(This is my first post after months of appreciative lurking…)

I’m trying to spec the following conditional controller macro:

class ApplicationController < ActionController::Base

turn off session management for robots

session :off, :if => lambda {|req| req.user_agent =~ /(Google|Slurp)/i
}

end

My current attempt seems to be quite unsuccessful:

  1. when I include the controller.should_receive(:session) statement, the
    index action no longer seems to be actually called (ie a breakpoint
    placed
    in the index method is not triggered)
  2. I receive the following error:
    Spec::Mocks::MockExpectationError in ‘ApplicationController should turn
    off
    session management for requests made by robot user agent’
    Mock ‘FooController’ expected :session with (:off) but received it with
    (no
    args)
    ./spec/controllers/application_spec.rb:19:

Any suggestions?

Thanks much,
Matt

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


View this message in context:
http://www.nabble.com/Spec'ing-controller-macros-tp16927457p16927457.html
Sent from the rspec-users mailing list archive at Nabble.com.

On Tue, Apr 29, 2008 at 9:48 AM, Matt McNeil [email protected]
wrote:

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
end

Pat