[mocha-developer] replacement for returning Proc

James Mead jamesmead44 at gmail.com
Wed Jun 13 12:46:45 EDT 2007

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()
  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.now # => time_1
  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.

More information about the mocha-developer mailing list