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
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. On 11/13/07 2:23 PM, "Matthew Lins" <mlins at webinforem.com> 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 > > 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 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
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) > endYou 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