【问题标题】:OHTTPS + AFNetworking Mocking the Failure ResponseOHTTPS + AFNetworking 模拟失败响应
【发布时间】:2015-08-04 16:16:04
【问题描述】:

我目前正在使用 OHTTPStub 来模拟我的 AFNetworking 调用的响应。 AFNetworking 调用返回成功响应或失败响应。

如果我使用“responseWithJSONObject”方法(这是提供或讨论的有关 OHTTPStubs 的唯一示例)创建存根,则始终执行我的 AFNetworking 调用的成功块。

那么,我需要调用什么方法来执行我的 AFNetworking 方法的失败块。

我已经尝试过OHTTPStubs的“responseWithError”方法,它既不调用成功也不调用失败块。

【问题讨论】:

    标签: ios unit-testing afnetworking ohhttpstubs


    【解决方案1】:

    在您提出请求之前尝试添加此内容。

    
    NSError *error = [NSError errorWithDomain:@"YourDomainName" code:444 userInfo:infoDictionary];
    
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES;
    } withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithError:error];
    }];
    

    这将存根所有请求并返回状态码为 444 的响应。The documentation for AFNetworking's acceptable status codes 表明 AFNetworking 符合 RFC 2616。The RFC 2616 documentation 表示状态码 400-499 应用于客户端错误,500-599 用于服务器错误。这意味着 AFNetworking 收到的任何状态码为 400-599 的响应都将执行失败块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2011-06-17
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多