# Trouble stubbing a method

**URL:** <https://www.ruby-forum.com/t/trouble-stubbing-a-method/120114>\
**Category:** RSpec\
**Created:** [November 13, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/trouble-stubbing-a-method/120114 "2007-11-13T21:24:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [November 13, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/trouble-stubbing-a-method/120114/1 "2007-11-13T21:24:43Z")

</div>

Hey guys,

I’ll try to explain this without the model code at first, since there is  
so  
much, but if need be, I’ll pastie it.

I have an Order model that makes API calls to a payment  
gateway(TrustCommerce) during validation(to verify the credit card  
information). I’d like to stub this behavior to avoid thousands of calls  
to  
TrustCommerce a day(autotest).

All the calls are made in one method: Order#payment\_authorize. This is  
called from ActiveRecord#validate.

So, my thinking was just to stub out Order#payment\_authorize. So here is  
one  
of my specs:

describe Order, “placed via the web” do

include OrderSpecHelper

before(:each) do  
@order = generate\_valid\_web\_order  
@order.should\_receive(:payment\_authorize)  
end

it do  
@order.should be\_valid  
end

end

I’m using a mock for now, just to verify it does get called, once it’s  
working I’ll change it to a stub. (I want to explicitly test it on  
another  
spec)

Anyhow, this doesn’t work, but sure enough, the calls out to  
TrustCommerce  
are being made. I also tried this:

describe Order, “placed via the web” do

include OrderSpecHelper

before(:each) do  
@order = generate\_valid\_web\_order  
Order.should\_receive(:payment\_authorize)  
end

it do  
@order.should be\_valid  
end

end

However, this didn’t work either. Just for clarity, this is how it’s  
being  
called in the model:

validate :payment\_authorize

How can I stub this method?

Thanks,

Matt L.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [November 13, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/trouble-stubbing-a-method/120114/2 "2007-11-13T21:27:03Z")

</div>

BTW…When I say, “this doesn’t work”

I mean the spec fails on the mock, saying the method was never called.  
But,  
indeed it was, I can verify it by the TrustCommerce logs.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [November 13, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/trouble-stubbing-a-method/120114/3 "2007-11-13T21:37:00Z")

</div>

On Nov 13, 2007, at 3:23 PM, Matthew L. wrote:

> TrustCommerce a day(autotest).  
> include OrderSpecHelper
> 
> before(:each) do  
> @order = generate\_valid\_web\_order  
> @order.should\_receive(:payment\_authorize)  
> end

You probably want @order.stub!(:payment\_authorize) - should\_receive  
is an assertion. It doesn’t belong in a before(:each)/setup block.

If you want to test that the payment\_authorize is called, you’ll want  
to pull the assertion that you gave below into your spec:

describe Order, “placed via the web” do

include OrderSpecHelper

before(:each) do  
@order = generate\_valid\_web\_order  
@order.stub!(:payment\_authorize).and\_return true  
end

it “should try to authorize the payment when validating the  
record” do  
@order.should\_receive(:payment\_authorize).and\_return true  
@order.should be\_valid  
end

end

Scott
