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

James Mead jamesmead44 at gmail.com
Sat Dec 2 15:53:09 EST 2006

On 02/12/06, Gabriel Birke <gabriel.birke at gmail.com> wrote:
> I didn't know you can "stack" expectations this easily - it makes may
> example somewhat moot.
> I assumed that each "expect" would override the previous one. Btw,
> does that mean that each "expect" call adds 1 to the expected number
> of times the mocked method is called?

There are two stages for an expectation, matching and verifying.

When a method is called on a mock, it checks through its list of
expectations looking for the first matching one. Expectations match
based on a combination of the method name and the parameters. If you
do not specify any parameters, i.e. no call to with(), the expectation
will match any parameters. When an expectation is matched, its count
is incremented.

At verification time, for each expectation the actual count is
compared to the expected count. So if you have two expectations for
the same method but with different parameters, each expectation has a
default expected count of 1. But you could set different expected
counts on each expectation...



Does that make sense?

One confusing aspect of the behaviour at present is when you specify
the same expectation twice with the same parameters - I need to do
some work on this...


object.my_method(1) # => 1
# first expectation passes verification - expected count 1, actual count 1
# second expectation fails verification - expected count 1, actual count 0

I think it makes most sense in this case for the second expectation to
override the first...

object.my_method(1) # => 2
# first expectation ignored
# second expectation passes verification - expected count 1, actual count 1

What do you think?

More information about the mocha-developer mailing list