jeudi 4 juin 2015

OCMock stub blocks

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.

Aucun commentaire:

Enregistrer un commentaire