Objective c OCMock和block
我有一个具有以下签名的方法,我想使用OCMock的存根特性对其进行测试:Objective c OCMock和block,objective-c,objective-c-blocks,ocmock,Objective C,Objective C Blocks,Ocmock,我有一个具有以下签名的方法,我想使用OCMock的存根特性对其进行测试: - (void)signInWithEmail:(NSString *)email andWithPassword:(NSString *)password andWithBlock:(void (^)(GNCustomer *customer, NSError *error))block 我将如何通过模拟来处理返回的块 我试过: [[_mockAuthenticationRepository stub] signInW
- (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) {
}];
当我尝试使用这种方法存根时,我收到了调用的意外方法,这表明我的存根没有被使用
谢谢 我想出来了:)答案如下:
[[[_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]];
atIndex选项还引用方法签名中的点,该块从第一个参数的索引2开始调用。