OCMock和Blocks

时间:2013-10-15 18:06:08

标签: objective-c objective-c-blocks ocmock

我有一个带有以下签名的方法,我想使用OCMock的存根功能进行测试:

- (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) {

            }];

当我尝试使用这种方法存根时,我收到了意外调用的方法,表明我的存根没有被使用。

谢谢!

1 个答案:

答案 0 :(得分:5)

好吧,我想出来了:)这就是答案:

[[[_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]];