Spec'ing controller macros

View: New views
2 Messages — Rating Filter:   Alert me  

Spec'ing controller macros

by Matt McNeil () :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Spec'ing controller macros

by Pat Maddox :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Apr 29, 2008 at 9:48 AM, Matt McNeil <nabble.108@...> 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
  end

Pat
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users