2

I am currently using OHTTPStub to mock the responses of my AFNetworking calls. The AFNetworking calls return either a successful response or a failing response.

If I create a stub using "responseWithJSONObject" method, which is the only example provided or discussed concerning OHTTPStubs, then the success block of my AFNetworking calls is always executed.

So, what method do I need to call in order to execute the failure block of my AFNetworking methods.

I have already tried OHTTPStubs's "responseWithError" method, which does not call either the success or the failure block.

Charlie Cliff
  • 109
  • 2
  • 4

1 Answers1

2

Try adding this before your request is made.


NSError *error = [NSError errorWithDomain:@"YourDomainName" code:444 userInfo:infoDictionary];

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return YES;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
    return [OHHTTPStubsResponse responseWithError:error];
}];

This will stub all requests and return a response with status code 444. The documentation for AFNetworking's acceptable status codes shows that AFNetworking conforms to RFC 2616. The RFC 2616 documentation says that status codes 400-499 should be used for client errors and 500-599 for server errors. This means that any response AFNetworking receives with status code 400-599 will execute the failure block.

Abizern
  • 146,289
  • 39
  • 203
  • 257