开发者

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

开发者 https://www.devze.com 2022-12-10 21:59 出处:网络
I want to test my User Session Controller testing that a user session is first built then saved. My UserSession class looks like this:

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 => "some_email@gmail.com", :password => "foobar")
        post :create, :user_session => { :email => "some_email@gmail.com", :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=>"some_email@gmail.com"})
       开发者_StackOverflowgot: ({: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.


This worked for me when I was getting extra :new messages on UserSession with ({:priority_record=>nil}, nil)

UserSession.should_receive(:new).at_least(1).times.with({ :email => "some_email@gmail.com", :password => "foobar" })
0

精彩评论

暂无评论...
验证码 换一张
取 消