3

我有一个带有以下签名的方法,我想使用 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) {

            }];

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

谢谢!

4

1 回答 1

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]];
于 2013-10-15T19:03:27.473 回答