[rspec-users] Trouble stubbing a method

Scott Taylor mailing_lists at railsnewbie.com
Tue Nov 13 15:36:38 EST 2007


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

> 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

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





More information about the rspec-users mailing list