0

我尝试存根这个 AFNetworking 方法并测试错误案例:

- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

我的测试方法如下:

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();
});

方法也[API getWeeklyEvents]使用 PromiseKit。这可能是一个问题吗?

我尝试使用,但它不起作用并使用旧的 OCMock 语法。

4

1 回答 1

1

因此,您已经setCompletionBlockWithSuccess:failure:使用一个实际调用故障处理程序的块来存根该方法。这意味着,就 OCMock 而言,在其他人调用setCompletionBlockWithSuccess:failure.

现在该方法是一个实例方法,必须在模拟上调用。唯一被调用的方法是getWeekEvents. 我不确定那将如何调用该 set 方法,因为它需要对模拟的引用。

老实说,我认为你必须重新设计你的测试。

于 2015-06-05T07:25:03.333 回答