문제

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!

도움이 되었습니까?

해결책

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]];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top