我有一个具有以下签名的方法,我想使用 OCMock 的 stub 功能进行测试:
- (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block
我将如何模拟它来处理返回的块。
我试过了:
[[_mockAuthenticationRepository stub] signInWithEmail:[OCMArg any] andWithPassword:[OCMArg any] andWithBlock:^(GNCustomer *customer, NSError *error) {
}];
当我尝试使用这种方法 stub 时,我收到了意外调用的方法,这表明我的 stub 未被使用。
谢谢!
最佳答案
好吧,我想通了:) 这是答案:
[[[_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]];
关于objective-c - OCMock 和块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19388051/