0

在以下示例中,@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
4

0 回答 0