【发布时间】:2015-06-04 22:36:11
【问题描述】:
我尝试存根这个 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]method 也使用 PromiseKit。会不会是个问题?
我尝试使用this,但它不起作用并使用旧的 OCMock 语法。
【问题讨论】:
标签: ios objective-c unit-testing ocmock promisekit