次の例では、@user.expects(:send_invitations!).once アサーションが失敗していますが、アプリによって招待状が送信され、@send_invitations 変数が割り当てられています。@user.send_invitations を期待しますか! @user.expects(:send_invitations!) が間違って使用されていますか?
コントローラー
class RegistrationsController < ApplicationController
before_filter :require_active_user
def welcome
if params[:send_invitations]
current_user.send_invitations!
@send_invitations = true
end
end
end
コントローラーのテスト
require 'test_helper'
class RegistrationsControllerTest < ActionController::TestCase
context "With a logged-in user" do
setup { login_as @user = Factory(:user) }
context ":welcome" do
context "with params[:send_invitations] present" do
setup { get :welcome, { :send_invitations => true } }
should "send invitations" do
@user.expects(:send_invitations!).once # tests say this isn't being invoked
assert assigns(:send_invitations) # but tests say this IS being assigned
end
end
context "without the presence of params[:send_invitations]" do
setup { get :welcome }
should "not send invitations" do
@user.expects(:send_invitations!).never # passes fine
assert !assigns(:send_invitations!) # also passes fine
end
end
end
end
end