ユーザーセッションが最初に構築されてから保存されることをテストするユーザーセッションコントローラーをテストしたいと思います。私の 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 を呼び出しても違いはありません。