Reputation: 640
I try to stub this AFNetworking method and test error case:
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
My test method look like:
it(@"should return error when remote api fails", ^{
id mock = OCMClassMock([AFHTTPRequestOperation class]);
OCMStub([mock setCompletionBlockWithSuccess:[OCMArg any] failure:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
void (^failureBlock)(AFHTTPRequestOperation *operation, NSError *error) = nil;
[invocation getArgument:&failureBlock atIndex:3];
NSDictionary *details = @{ NSLocalizedDescriptionKey : [OCMArg any] };
NSError *err = [NSError errorWithDomain:@"Some Domain" code:401 userInfo:details];
failureBlock(nil, err);
});
[API getWeeklyEvents].catch(^(NSError *err) {
error = [err copy];
});
expect(error).will.beTruthy();
});
Also [API getWeeklyEvents]
method uses PromiseKit. Can it be an issue?
I tried to use this but it doesnt work and uses old OCMock syntax.
Upvotes: 0
Views: 608
Reputation: 3014
So, you've stubbed the setCompletionBlockWithSuccess:failure:
method with a block that actually calls the failure handler. This means, as far as OCMock is concerned, that nothing will happen until someone else calls setCompletionBlockWithSuccess:failure
.
Now that method is an instance method and would have to be invoked on the mock. The only method that is called is getWeekEvents
. I'm not sure how that would be able to call that set method because it would need a reference to the mock.
To be honest, I think you have to redesign your test.
Upvotes: 1