Reputation: 1993
I have a method with the following signature that I want to test using OCMock's stub feature:
- (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block
How would I go about mocking this to handle the returned block.
I have tried:
[[_mockAuthenticationRepository stub] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:^(GNCustomer *customer, NSError *error) {
}];
When I try to stub with this approach, I received unexpected method invoked which indicates that my stub is not being used.
Thanks!
Upvotes: 3
Views: 507
Reputation: 1993
Well I figured this out :) Here's the answer:
[[[_mockAuthenticationRepository stub] andDo:^(NSInvocation *invocation) {
void (^successBlock)(GNCustomer *customer, NSError *error) = nil;
[invocation getArgument:&successBlock atIndex:4];
NSDictionary *details = @{ NSLocalizedDescriptionKey : [OCMArg any] };
NSError *error = [NSError errorWithDomain:@"Some Domain" code:401 userInfo:details];
successBlock(nil, error);
}] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:[OCMArg any]];
Upvotes: 5