如何使用存根指定authlogic会话控制器?

时间:2009-11-03 10:13:45

标签: ruby-on-rails rspec authlogic

我想测试我的用户会话控制器测试,首先构建用户会话然后保存。我的UserSession类看起来像这样:

class UserSession < Authlogic::Session::Base
end

我的UserSessionsController的create方法如下所示:

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

我的控制器规格如下:

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

我存在新方法,但在运行测试时仍然出现以下错误:

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"})
       got: ({:priority_record=>nil}, nil)

虽然在我的控制器代码被调用之前,在UserSession上调用了新方法。调用activate_authlogic没有任何区别。

1 个答案:

答案 0 :(得分:0)

当我获得额外功能时,这对我有用:UserSession上的新消息({:priority_record =&gt; nil},nil)

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