Last active
December 15, 2015 12:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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