我想在rspec测试中使用Mocks.
klass.any_instance.should_receive(:save).exactly(2).times.and_return(true)
但是我收到一条错误消息,如:
‘消息“保存”由<#Object>但是已经被<#Object>
临时我使用存根,但准确性要使用嘲笑
解决方法
any_instance.should_receive的
documentation是:
Use any_instance.should_receive to set an expectation that one (and only one) instance of a class receives a message before the example is completed.
所以你已经指定一个对象应该接收两次保存调用,而不是这两个对象应该接收一次保存调用.
如果您想计算不同实例的呼叫,则必须为creative:
save_count = 0 klass.any_instance.stub(:save) { save_count+=1 } # run test save_count.should == 2