How can I spec out an authlogic sessions controller using using a stub?

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2009-11-03T10:13:45Z Indexed on 2010/05/25 20:01 UTC
Read the original article Hit count: 239

Filed under:
|
|

I want to test my User Session Controller testing that a user session is first built then saved. My UserSession class looks like this:

class UserSession < Authlogic::Session::Base
end

The create method of my UserSessionsController looks like this:

def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
        flash[:notice] = "Successfully logged in."
        redirect_back_or_default administer_home_page_url
    else
        render :new
    end
end

and my controller spec looks like this:

describe UserSessionsController do

    it "should build a new user session" do
        UserSession.stub!(:new).with(:email, :password)
        UserSession.should_receive(:new).with(:email => "[email protected]", :password => "foobar")
        post :create, :user_session => { :email => "[email protected]", :password => "foobar" }
    end
end

I stub out the new method but I still get the following error when I run the test:

Spec::Mocks::MockExpectationError in 'UserSessionsController should build a new user session'
<UserSession (class)> received :new with unexpected arguments
  expected: ({:password=>"foobar", :email=>"[email protected]"})
       got: ({:priority_record=>nil}, nil)

It's although the new method is being called on UserSession before my controller code is getting called. Calling activate_authlogic makes no difference.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rspec