[mocha-developer] Terse Mocks

Brian Takita brian.takita at gmail.com
Wed May 2 05:38:21 EDT 2007


Hello,

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

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)

The patch is pretty simple too.


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)
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,
Brian Takita


More information about the mocha-developer mailing list