ios - OHTTPS + AFNetworking 模拟失败响应

标签 ios unit-testing afnetworking ohhttpstubs

我目前正在使用 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/

相关文章:

ios - 保留和分配有什么区别

unit-testing - 在 Moq 中,是否有一种简单的方法可以使 Setup() 返回 null,而不管提供的所有参数如何?

c# - 在这种情况下编写好的单元测试

ios - 使用 Instapaper API 时,为什么我不断收到 401 错误?

ios - 定时循环结果

ios - iPhone:如何将 inputAccessoryView 修复为 View?

unit-testing - 此时缺少对模拟类型的调用;

objective-c - AFNetworking 证书错误

ios - 由于 NSURLSession/NSURLConnection HTTP 加载失败,无限 pthread 创建崩溃应用程序

iOS Instruments Leaks 一旦应用程序启动就停止录制