[rspec-users] Trouble stubbing a method

Matthew Lins mlins at webinforem.com
Tue Nov 13 15:23:29 EST 2007


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 Lins




More information about the rspec-users mailing list