[mocha-developer] replacement for returning Proc

Justin Perkins justin at spiceworks.com
Wed Jun 13 13:11:32 EDT 2007


Thanks for your reply James.

As you have discovered by looking at the example, the method we are  
trying to mock is not simply returning a basic value, it is calling  
the Rails render method. When this method (the one which calls the  
render method) is mocked, then no call to render occurs and Rails  
throws an exception because every action MUST have a render of some  
kind (unless I am mistaken).

The obvious solution here is to re-factor our code to not rely on the  
render method being called from the helper method and instead have it  
return a string of text, which it will then pass to the render  
method. Unfortunately this is not the ideal solution for us at this  
time since it involves changing quite a bit of code.

For now we can deal with deprecation warnings until a time (hopefully  
in a month or so) when we can clean up this code to not be so  
dependent on the render method.

Thanks again,
justin

On Jun 13, 2007, at 11:46 AM, James Mead wrote:

> Hi Justin,
>
> Thanks for your question. It's great to have an example test to  
> talk around.
>
> As you've noticed, using Expectation#returns with an instance of a  
> Proc is
> deprecated in version 0.5 of Mocha. There is no drop-in replacement  
> for this
> usage. Let me try and explain why...
>
> 1) Currently you can't easily return an instance of a Proc. Once  
> the current
> usage has been removed in the next release, we'll be able to do the
> following which seems more logical and consistent...
>
>   proc = Proc.new
>   object = mock()
>   object.stubs(:method).returns(proc)
>   assert_equal proc, object.method
>
> 2) The current usage encourages the use of complex logic for computing
> return values which is not sensible mocking practice. Return values  
> should
> really be explicitly defined by the test. The newer version of  
> Mocha gives
> much more flexibility in doing this e.g. you can do...
>
>   Time.stubs(:now).returns(time_1)
>   Time.now # => time_1
>   Time.stubs(:now).returns(time_2)
>   Time.now # => time_2
>
>   m = mock()
>   m.stubs(:method).returns(1, 2, 3).then.raises 
> ('error').then.returns(5, 6)
>
> Although the ideas above will solve many scenarios, they won't  
> directly
> solve your problem.
>
> In your example, you expect a call to the operation_with_render  
> method, but
> still want some of the side effects of the method to occur (i.e.  
> the call to
> the render method). Can you explain more about why you need this?  
> Can you
> flesh out your example a little bit more, so I understand what you are
> trying to achieve. Alternatively could you move the call to the
> rendermethod outside the
> operation_with_render method?
>
> Presumably your example test is a Rails controller test in real  
> life...?
>
> Cheers, James.
> -- 
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer



More information about the mocha-developer mailing list