ruby-on-rails – Rspec Mocks在any_instance与正好(n)次
发布时间:2020-12-16 22:01:40 所属栏目:百科 来源:网络整理
导读:我想在rspec测试中使用Mocks. klass.any_instance.should_receive(:save).exactly(2).times.and_return(true) 但是我收到一条错误消息,如: ‘消息“保存”由#Object但是已经被#Object 临时我使用存根,但准确性要使用嘲笑 解决方法 any_instance.should_rece
我想在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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |