[mocha-developer] If I stub do I have to mock as well?
James Mead
jamesmead44 at gmail.com
Fri Oct 13 16:48:23 EDT 2006
On 13/10/06, Tim Case <gk at karmacrash.com> wrote:
>
> James, just figured it out and everything works! Here's what was going
> on:
>
> class Order < ActiveRecord::Base
> def successful?
> end
> end
>
> class CcOrder < Order
> end
>
> def test_post_checkout
> Order.any_instance.stubs(:successful?).returns(true)
> post :checkout
> assert_response :redirect
> assert_equal "Checkout was successful.", flash[:notice]
> end
>
> CcOrder is the concrete class that was actually getting called in the
> process_order method inside the store controller code. In the test, I
> was stubbing out the method from CcOrder's parent class Order, by
> changing the stub to operate on CcOrder the test worked as expected.
>
> def process_order
> if @order.process and @order.successful? /*<-- at order is a CcOrder
> not Order*/
> @cart.empty!
> if customer_logged_in?
> flash[:notice] = "Checkout was successful."
> else
> flash[:notice] = "Checkout was successful, and your account was
> created."
> end
> redirect_to_order_confirmation @order.id
> return
> else
> render_errors_in_order_summary
> end
> end
>
> def test_post_checkout
> CcOrder.any_instance.stubs(:successful?).returns(true)
> post :checkout
> assert_response :redirect
> assert_equal "Checkout was successful.", flash[:notice]
> end
Great - I'm glad you worked it out - it all makes sense now.
It seems to me that the original call to stub Order should have
> propagated down to Order's child classes as well. Is this by design or
> something for the future? (Maybe I can help)
It's not by design - more by accident. We don't tend to use the
any_instance() functionality much, so I guess we just haven't come across a
case like this. Maybe it is something we should support.
A new mocha fan! Nice work James!!!
Thanks - I'm glad you're finding it useful.
--
James.
http://blog.floehopper.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061013/32e262a0/attachment.html
More information about the mocha-developer
mailing list