我目前正在使用 OHTTPStub 来模拟 AFNetworking 调用的响应。 AFNetworking 调用返回成功响应或失败响应。
如果我使用“responseWithJSONObject”方法创建一个 stub (这是有关 OHTTPStubs 提供或讨论的唯一示例),那么我的 AFNetworking 调用的成功 block 始终会被执行。
那么,我需要调用什么方法才能执行 AFNetworking 方法的失败 block 。
我已经尝试过 OHTTPStubs 的“responseWithError”方法,该方法不会调用成功或失败 block 。
最佳答案
尝试在提出请求之前添加此内容。
NSError *error = [NSError errorWithDomain:@"YourDomainName" code:444 userInfo:infoDictionary];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithError:error];
}];
这将 stub 所有请求并返回状态代码为 444 的响应。The documentation for AFNetworking's acceptable status codes显示 AFNetworking 符合 RFC 2616。The RFC 2616 documentation表示状态代码 400-499 应用于客户端错误,500-599 应用于服务器错误。这意味着 AFNetworking 收到的任何状态代码为 400-599 的响应都将执行失败 block 。
关于ios - OHTTPS + AFNetworking 模拟失败响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400109/