On Apr 28, 2011, at 4:37 PM, David Kahn wrote: > I am a bit new to mocking. I am trying to stub the > ActiveMerchant::Billing::PaypalGateway#authorize method but not clear how to > do it. This is my code and spec. > > This is the pertinent code: > > module Payment > def gateway > ActiveMerchant::Billing::PaypalGateway.new( > ... > ) > end > > def authorize_payment(payment_info, associated_record_type, > associated_record_id) > gateway.authorize(payment_info.amount ...... > end > > I tried this: > ActiveMerchant::Billing::PaypalGateway.should_receive(:authorize).and_return(authorize_payment_success_response)
This is setting an expectation on the PaypalGateway object (which is a class!). But when you call PaypalGateway.new, you get back an instance - which is where you want to set the expectation. So what you really need to stub is something that looks more like an instance...you'd start off with: gateway = stub('gateway') gateway.should_receive(:authorize) and next you can either stub PaypalGateway.new: ActiveMerchant::Billing::PaypalGateway.stub(:new).and_return gateway or what I'd more likely do: Payment.stub(:gateway).and_return gateway either one will get you there. Pat _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users