[mocha-developer] If I stub do I have to mock as well?

Tim Case gk at karmacrash.com
Fri Oct 13 12:12:06 EDT 2006

James Mead wrote:
> On 13/10/06, *Tim Case* <gk at karmacrash.com <mailto:gk at karmacrash.com>> 
> wrote:
>     Hi, I'm new to mocha and stubba but eager to learn.  I have a rails
>     functional test in which I would like to stub an instance method of
>     particular class to always return true.  I tried the following:
>     def test_post_checkout
>         Order.any_instance.stubs(:successful?).returns(true)
>         post :checkout
>         assert_response :redirect
>         assert_equal "Checkout was successful.", flash[:notice]
>     end
>     I expected that the stub would help my test pass but instead it
>     failed
>     and the failure included the following:
>          test/functional/store_controller_test.rb:103:in
>     `test_post_checkout']:
>     #<Mocha::Mock: 28754118>.successful?() - expected calls: 0, actual
>     calls: 1
>     I was confused by this and it seemed to indicate that I need to tell
>     Order that a call to #successful? was expected.  Do I have to create a
>     mock in order to stub?  How do I do this?
>     Thanks,
>     Tim Case
>     tim at karmacrash.com <mailto:tim at karmacrash.com>
> Hi Tim,
> A first glance your test looks fine and it sounds like you understand 
> how it should work. Can you send the code for your checkout action? 
> Also can you tell me whether you are using the gem (which version?) or 
> the Rails plugin (which revision?) - I've been making a few fairly big 
> changes in HEAD recently and I may have inadvertantly introduced a bug 
> to the Rails plugin.
> -- 
> James.
> http://blog.floehopper.org
> ------------------------------------------------------------------------
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
Hi James, thanks for your help I installed mocha as a rails plugin from 
revision 70.

the checkout action calls process order:

 def checkout
    @order = Order.new_order(@store.bill_type.name, params[:order])
    @customer = find_or_create_customer
    @order.store = @store
    @line_items = @cart.line_items
    @order.line_items << @cart.line_items
     if @order.valid? and @customer.valid?
      append_to_order_errors @customer.errors

  def process_order
    if @order.process and @order.successful?    #<---This is what I want 
to stub
      if customer_logged_in?
        flash[:notice] = "Checkout was successful."
        flash[:notice] = "Checkout was successful, and your account was 
      redirect_to_order_confirmation @order.id

@order.successful? is what I would like to return a true when I specify 
it and at times false when I specify it.

More information about the mocha-developer mailing list