1

我想测试我的用户会话控制器,测试用户会话首先构建然后保存。我的 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 没有区别。

4

1 回答 1

0

当我收到额外的:UserSession 上的新消息时,这对我有用 ({:priority_record=>nil}, nil)

UserSession.should_receive(:new).at_least(1).times.with({ :email => "some_email@gmail.com", :password => "foobar" })
于 2010-05-25T19:59:43.083 回答