Rnk*_*gir 12 rspec ruby-on-rails rspec-rails
我想在rspec测试中使用Mocks.
klass.any_instance.should_receive(:save).exactly(2).times.and_return(true)
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息:
'消息"save"由<#Object>接收,但<#Object>已收到
临时我使用存根但准确性要使用模拟
Szt*_*upY 20
该文件的any_instance.should_receive
方法是:
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.
Run Code Online (Sandbox Code Playgroud)
所以你已经指定了一个对象应该接收save
两次调用,而不是那两个对象应该接收save
一次调用.
如果您想计算不同实例所做的调用,您必须具有以下创造性:
save_count = 0
klass.any_instance.stub(:save) { save_count+=1 }
# run test
save_count.should == 2
Run Code Online (Sandbox Code Playgroud)