我编写以下规格:故障使用RSpec与方法
it "should call user.invite_friend" do user = mock_model(User, :id => 1) other_user = mock_model(User, :id => 2) User.stub!(:find).with(user.id).and_return(user) User.stub!(:find).with(other_user.id).and_return(other_user) user.should_receive(:invite_friend).with(other_user) post :invite, { :id => other_user.id }, {:user_id => user.id} end
但我发现了以下错误,当我运行规范
NoMethodError in 'UsersController POST invite should call user.invite_friend' undefined method `find' for # Class:0x86d6918 app/controllers/users_controller.rb:144:in `invite' ./spec/controllers/users_controller_spec.rb:13:
有什么错误?没有.with它工作得很好,但我希望stub方法的不同参数有不同的返回值。以下控制器的操作可能是相关的:
def invite me.invite_friend(User.find params[:id]) respond_to do |format| format.html { redirect_to user_path(params[:id]) } end end def me User.find(session[:user_id]) end
“这并没有回答这个更大的问题:我怎么存根多种方法与参数调用根据我的经验,你不能那样做。“存根方法可以接收参数为参数的块,并且可以根据该参数的值返回不同的对象。 – arieljuod 2017-10-19 22:00:07