"If I find that I would like to ignore a lot of calls between the object = under test and a mock, expecially if I'd like a wildcard "ignore = everything else", then I take that as an indication that the = communication between them is too complicated and fiddly and try to = simplify it."
I think that this is a very important point, when this happens I also = view it as a design smell, highlighted by the test. I found that a lot = of people simply deal with the symptoms by adding features to the mock = framework to ignore the unexpected calls, rather than looking at the = underlying problem. In fact this has become the default behaviour in = most of the mock frameworks in the .NET world, by default these mock = frameworks will ignore all calls which are not explicitly specified.
Mock object are most useful to desing communication protocols between = objects, if the there is a weakness in communication protocol between = objects the tests will let you know very quickly.
Isaiah
-----Original Message-----
From: Nat Pryce [mailto:
nat.pryce@...]
Sent: Monday, March 30, 2009 1:14 PM
To:
user@...
Subject: Re: [jmock-user] Ignoring all mock method calls EXCEPT certain ones
2009/3/30 Lawson, Robert W <
robert.w.lawson@...>:
> Nat, I really appreciate the fact that you took the time to respond in detail. Thanks!! Just curious, is the reason you never found a need for this because you confirm in your tests every call made by the object-under-test to the mock?
If I find that I would like to ignore a lot of calls between the object under test and a mock, expecially if I'd like a wildcard "ignore everything else", then I take that as an indication that the communication between them is too complicated and fiddly and try to simplify it. It might be that responsibilities are not distributed clearly among different objects, or that an object is not hiding enough of its implementation details. Whatever the reason (or, more likely, several reasons at once), I'll try to improve the design so that the problem doesn't arise in the tests.
> Also, do you use jMock to test anonymous classes? If so, how do you do that?
Anonymous classes are internal implementation details of an object, so they get tested along with the object. I don't like to give them to any behaviour except forwarding their calls to private methods of the object that creates them.
--Nat
>
> -----Original Message-----
> From: Nat Pryce [mailto:
nat.pryce@...]
> Sent: Friday, March 27, 2009 6:35 PM
> To:
user@...
> Subject: Re: [jmock-user] Ignoring all mock method calls EXCEPT
> certain ones
>
> Sorry about the lack of detail in my last mail. I sent it from my
> iPhone and it's not easy to write lots of text.
>
> The following link describes how jMock dispatches a method calls to
> expectations and explains why oneOf and allowing don't interact how
> you expect.
>
>
http://www.jmock.org/dispatch.html>
> The following link explains how you can use Matchers to specify which
> mock objects and methods match an expectation. You can use that to
> match all methods of a mock object except those you've already
> specified expectations for.
>
>
http://www.jmock.org/match-object-or-method.html>
> It's not ideal: jMock could perhaps have an API call to let a test
> easily specify "all other methods of object o". It's not something
> that I've ever found a need for in practice so I've never considered
> it. I'll have a think about how it could be implemented.
>
> --Nat
>
>
http://www.natpryce.com>
>
>
> 2009/3/27 Lawson, Robert W <
robert.w.lawson@...>:
>> Thanks for the reply Nat, but I don't understand how these links help
>> to solve my problem. Would you be able to modify the example I gave
>> below to show me what you mean? Thanks!
>>
>>
>>
>> From: Nat Pryce [mailto:
nat.pryce@...]
>> Sent: Friday, March 27, 2009 3:42 PM
>> To:
user@...
>> Cc:
user@...; Romano, Anthony D
>> Subject: Re: [jmock-user] Ignoring all mock method calls EXCEPT
>> certain ones
>>
>>
>>
>> Please read
>>
>>
>>
>>
http://www.jmock.org/dispatch.html>>
>>
>>
>> And
>>
>>
>>
>>
http://www.jmock.org/match-object-or-method.html>>
>>
>>
>> Cheers,
>>
>> Nat
>>
>> www.natpryce.com
>>
>> On 27 Mar 2009, at 15:15, "Lawson, Robert W"
>> <
robert.w.lawson@...>
>> wrote:
>>
>> I want to set an expectation that a particular method (or methods) on
>> a mock are called, but ignore any other methods that are called on
>> that mock. I've read some posts that say this is easily done by something like:
>>
>> oneOf (foo).doIt(45);
>>
>> ignoring (foo);
>>
>> However, this ignores all calls to the foo mock, including the doIt()
>> method. The only thing I can do right now to get my test to work is
>> to not use ignoring, and set an expectation on EVERY method of foo that is called.
>> If I don't, when any method other than doIt() is called, an
>> unexpected method invocation exception is thrown.
>>
>> I'm sure there has to be an easy answer to this. Can anyone help?
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>
http://xircles.codehaus.org/manage_email>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>
http://xircles.codehaus.org/manage_email>
>
>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email