我尝试存根这个 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 语法。