Using OCMockito and OCHamcrest, I can set up expectations on the arguments to mocked methods, thusly:
[verify(aMockObject) doSomething:allOf(is(instanceOf([NSArray class])), hasCountOf(3U), nil)];
There doesn't seem to be an equivalently simple way to do this using Kiwi. It is possible to capture arguments using a spy, something like:
KWCaptureSpy *spy = [aMockObject captureArgument:@selector(doSomething:) atIndex:0];
NSArray *capturedArray = spy.argument;
And then to check expectations on the captured object:
[[capturedArray should] haveCountOf:3U];
Is there a less clumsy way to do this in Kiwi?
(I'm aware I could probably use hamcrest matchers in here, but for the moment I'm exploring what Kiwi is capable of).
One option that I have used is
stub:withBlock:
This works fine, and I find it easier to implement than the spy pattern. But your question made me wonder about expectations using message patterns. For example:
The first example will verify that
aMockObject
received thedoSomething:
message with an argument thatisEqual:myArray
. The second example will simply verify thatdoSomething:
was sent, with no expectation about the array arugment. It would be great if we can specify some type of Matcher in the message pattern, to express that we don't care what specific array instance is sent in the message, just that it has acount
of 3.I haven't found any examples of being able to do this, but it looks like there are some possibilities. To verify a message-sending expectation, Kiwi uses the
KWMessagePattern
class, specifically thematchesInvocation:
andargumentFiltersMatchInvocationArguments:
methods. This checks for three types of "argument filters":myArray
in the example above), which are compared to the actual value sent in the message usingisEqual:
KWAny
(such as theany()
macro in the example above), which will match any argument value[KWGenericMatchEvaluator isGenericMatcher:argumentFilter]
, which basically means that the object responds tomatches:(id)obj
Thus, you should be able to use objects that implement
matches:
in message-pattern expectations to do things like verify the length of arrays sent to stubbed methods, without resorting to spys or blocks. Here's a very simple implementation: (available as a Gist)It would be nice to be able to reuse Kiwi's built-in matcher classes here, but I haven't yet found out exactly how to do this.