[mocha-developer] Terse Mocks

James Mead jamesmead44 at gmail.com
Wed May 2 11:35:45 EDT 2007


On 02/05/07, Brian Takita <brian.takita at gmail.com> wrote:
>
> I recently made a feature request with a patch for terser mocks.
>
> http://rubyforge.org/tracker/index.php?func=detail&aid=10412&group_id=1917&atid=7480


Thanks for the feature request and especially for going to the trouble of
writing a patch.
I'm sorry for not responding - I've been busy with as usual.

Here are some examples with their current equivalents:
>
> the_mock.expects.foo(1,2) {|a, b| :bar}
> the_mock.expects.foo(1,2).returns(:bar)
> the_mock.expects(:foo).with(1,2) {|a, b| :bar}
> the_mock.expects(:foo).with(1,2).returns(:bar)


Hmm. I quite like the syntax of the first half, e.g.
"the_mock.expects.foo(1,2)", because it's similar to how the expected method
would be called. But I'm not so keen on the block syntax for the return
value, e.g. "{|a, b| :bar}", because I don't think it's as readable.

The patch is pretty simple too.


Sorry, I haven't had a chance to look at the code in the patch yet.

Also, here is an example from my feature request for mock blocks that does
> not have a patch yet.
>
> http://rubyforge.org/tracker/index.php?func=detail&aid=10413&group_id=1917&atid=7480
>
> the_mock = mock('The new mock') do
>   foo(1,2) {:bar}
>   baz(3,4).returns(:foobar)
> end
>
> In the previous block, expectations for foo and baz are being set.
> This block is equivalent to:
>
> the_mock = mock('The new mock')
> the_mock.expects(:foo).with(1,2) {:bar}
> the_mock.expects(:baz).with(3,4).returns(:foobar)


I've been thinking of doing something similar, but I would like to retain
the clear distinction between expecting and stubbing...

larry = mock() do
  named('Larry')
  responds_like(Lamb.new)
  expects(:baa)
  stubs(:meh)
end

Here is an ActiveRecord type example of the mock block syntax:
> User.mocks do
>   find('3').once {user}
>   destroy(7)
> end
>
> I don't think this would be too difficult to implement either.
>
> WDYT?
>

Thanks for your suggestions. What does anybody else think?

-- 
James.
http://blog.floehopper.org


More information about the mocha-developer mailing list