1

I have implemented example from Mocking Network Requests With OHHTTPStubs. Unfotunately I encountered EXC_BAD_ACCESS exception when matching my result in line:

 [[expectFutureValue(origin) shouldEventuallyBeforeTimingOutAfter(3.0)] equal:@"111.222.333.444"];

Did anyone encountered this kind of problem? What could be possible solution?

Here is the full code:

#import "Kiwi.h"
#import "AFNetworking.h"
#import "OHHTTPStubs.h"
#import "OHHTTPStubsResponse.h"

SPEC_BEGIN(NetworkTest)

describe(@"The call to the external service", ^{

    beforeEach(^{
        [OHHTTPStubs addRequestHandler:^OHHTTPStubsResponse*(NSURLRequest *request, BOOL onlyCheck){
            return [OHHTTPStubsResponse responseWithFile:@"test.json" contentType:@"text/json" responseTime:1.0];
         }];
     );

    it(@"should return an IP address", ^{

        __block NSString *origin;
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://httpbin.org/ip"]];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            origin = [JSON valueForKeyPath:@"origin"];
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)     {
            // no action
        }];

        [operation start];

        [[expectFutureValue(origin) shouldEventuallyBeforeTimingOutAfter(3.0)] equal:@"111.222.333.444"];

    });

});

SPEC_END 
pro_metedor
  • 1,176
  • 10
  • 17

1 Answers1

1

The test does not find the file test.json so it returns so that is why you get the nil.

Create a file test.json in the same folder as your test file and put the body you need to see the test pass or fail.

To see the test fail

{ "origin" : "1.2.3.4"}

To see the test pass

{ "origin" : "111.222.333.444"}

//Note add request handler has been deprecated the following will work

[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
    return YES; // Stub ALL requests without any condition
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
    // Stub all those requests with our "response.json" stub file
    return [OHHTTPStubsResponse responseWithFile:@"test.json" contentType:@"text/json" responseTime:1.0];
}];
AliSoftware
  • 32,623
  • 6
  • 82
  • 77
onekilo79
  • 106
  • 1
  • 4