Skip to content

Instantly share code, notes, and snippets.

@mmertsock
Last active December 15, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmertsock/5260189 to your computer and use it in GitHub Desktop.
Save mmertsock/5260189 to your computer and use it in GitHub Desktop.
An example of using a matcher pattern in Kiwi message-pattern expectations. See http://stackoverflow.com/questions/15262264/how-to-set-expectations-on-parameters-to-mocked-methods-in-kiwi
// A reusable class that satisfies isGenericMatcher:
@interface SOHaveCountOfGenericMatcher : NSObject
- (id)initWithCount:(NSUInteger)count;
- (BOOL)matches:(id)item; // this is what KWMessagePattern looks for
@property (readonly, nonatomic) NSUInteger count;
@end
@implementation SOHaveCountOfGenericMatcher
- (id)initWithCount:(NSUInteger)count
{
if (self = [super init]) {
_count = count;
}
return self;
}
- (BOOL)matches:(id)item
{
if (![item respondsToSelector:@selector(count)])
return NO;
return [item count] == self.count;
}
@end
// Your spec:
it(@"should receive an array with count 3", ^{
NSArray* testArray = @[@"a", @"b", @"c"];
id arrayWithCount3 = [[SOHaveCountOfGenericMatcher alloc] initWithCount:3];
id aMockObject = [SomeObj nullMock];
[[[aMockObject should] receive] doSomething:arrayWithCount3];
[aMockObject doSomething:testArray];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment