user1717344
user1717344

Reputation: 133

RSpec stubbing issue -- stub seems to be working but not being received

I'm new to RSpec so bear with me. I have an object with an order and I'm stubbing a method on that object. I call another method on that object, which then calls the method I'm stubbing (and that stub seems to be working because I placed debuggers around said method call and its returning properly. Plus I threw in a debugger in the actual method to be called; that wasn't hit so it seems to be stubbing all right).

But when I call @order.should_receive I get "expected :blah_method with (any args) once, but received it 0 times."

I'm not sure why should_receive isn't working and I'm not sure what I'm doing wrong. Any help? By the way, I'm on 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

Upvotes: 1

Views: 483

Answers (2)

Timbinous
Timbinous

Reputation: 2983

should_receive pretty much acts as a stub. You set that before you execute your method. So for the test to work you would do this.

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

Upvotes: 1

arieljuod
arieljuod

Reputation: 15838

should_receive goes BEFORE the call, and you don't need stub if you use should_receive (you can stub it right there)

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

Upvotes: 2

Related Questions