[mocha-developer] Scoping expectations and more ...

James Mead jamesmead44 at gmail.com
Sat Dec 2 10:15:37 EST 2006


On 02/12/06, Gabriel Birke <gabriel.birke at gmail.com> wrote:
> I'm new to Mocha and through experimentation I found out something
> that is not explicitly stated in the documentation: If you need to
> mock the same function call with different parameters and results, you
> can set the scope of expectations with a begin ... end block.
>
> I also made up an example on how to expect more than one function call
> with different parameters

Thanks for your interest in Mocha. It's quite likely that some
behaviour is not properly documented. I might be missing something,
but I don't see why you wouldn't write the two tests like this...

 def test_m_call
   b = B.new
   A.any_instance.expects(:m).with(5).returns(6)
   A.any_instance.expects(:m).with(9).returns(10)
   assert_equal 10, b.m(9)
   assert_equal 6, b.m(5)
 end

 def test_twocall
   b = B.new
   A.any_instance.expects(:m).with(6).returns(7)
   A.any_instance.expects(:m).with(7).returns(8)
   assert_equal 15, b.calltwice(5)
 end

I don't think the begin...end combinations in your first test buy you
anything, does it?

While your second test is an example of how to use a block with the
with() method and a Proc with the returns() method, I don't see why
you wouldn't just use two expectations as shown above.

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


More information about the mocha-developer mailing list