RSpec存根问题 - 存根似乎正在工作但没有收到

时间:2013-11-14 15:43:18

标签: ruby-on-rails ruby rspec

我是RSpec的新手所以请耐心等待。我有一个订单的对象,我在该对象上存根方法。我在该对象上调用另一个方法,然后调用我正在存根的方法(并且该存根似乎正在工作,因为我将调试器放在所述方法调用周围并正确返回。另外,我在实际方法中调试了一个调试器被叫;没有被击中,所以它好像是顽固的。)

但是当我打电话给@ order.should_receive时,我得到“期望:blah_method(任何args)一次,但收到它0次。”

我不确定为什么re_receive不起作用,我不确定我做错了什么。有帮助吗?顺便说一下,我正在使用RSpec 1.3.2。

 it 'should be called when blah blah blah' do
  @order.stub!(:blah_method).and_return true
  #import_foobar_order calls @order.blah_method
  #order_hash is irrelevant here, just a json obj converted to a hash
  @order.import_foobar_order(@order, order_hash, website)
  @order.should_receive(:blah_method).at_least(:once)
end

2 个答案:

答案 0 :(得分:2)

should_receive在调用之前进行,如果你使用了should_receive,你就不需要存根(你可以在那里存根)

it 'should be called when blah blah blah' do
  @order.should_receive(:blah_method).at_least(:once).and_return(true)
  @order.import_foobar_order(@order, order_hash, website)
end

答案 1 :(得分:1)

should_receive几乎充当存根。在执行方法之前设置它。因此,为了使测试起作用,你会这样做。

it 'should be called when blah blah blah' do
  @order.should_receive(:blah_method).at_least(:once).and_return(true)
  @order.import_foobar_order(@order, order_hash, website)
end