OHHTTPStubs: Stub not being called

I’m setting up a stub in an Specta spec that isn’t getting called. Here’s the code I’m using:

it(@"should request splashes", ^{
    __block BOOL success = NO ;

    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return [request.URL.host isEqualToString:@"zamba.cs.vt.edu"];
    }
                        withStubResponse: ^OHHTTPStubsResponse *(NSURLRequest *request) {
                            NSData *stubData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"findSplashesResponse" ofType:@"txt"]];
                            NSDictionary *standardHeaders = @{ @"Content-Type":@"text/html"};
                            return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:standardHeaders];
                        }].name = @"findSplashes.php";
    [OHHTTPStubs setEnabled:YES];

    [client POST:@"findSplashes.php"
      parameters:@{ @"hash": [client hashString],
                    @"lat":[NSString stringWithFormat:@"%f", vt.coordinate.latitude],
                    @"long":[NSString stringWithFormat:@"%f", vt.coordinate.longitude],
                    @"radius":@"1600" }
         success:^(NSURLSessionDataTask *task, id responseObject) {
             NSString *response = [[NSString alloc] initWithData:(NSData *) responseObject encoding:NSUTF8StringEncoding];
             success = [response isEqualToString:@"252\t5527.5387976613\t4\t4\t1387655919\t1387568319\t300\t40.739983\t-73.992951\n"] ? YES : NO;
         }
         failure:^(NSURLSessionDataTask *task, NSError *error) {
             success = NO;
         }];

    expect(success).will.beTruthy();
});

client is a singleton instance of an AFHTTPSessionManager from AFNetworking 2.0. Strangely, this test and other similar tests were passing originally–I don’t know what I’ve done to break them! When the POST request is made, I can check that OHHTTPStubs.sharedInstance is the same as when the stub was created. I’ve added the +[OHHTTPStubs setEnabled:] call per other issues here. The block passed as thestubRequestsPassingTest:` parameter is never executed–all requests hit the network. Any ideas? Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 31 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@AliSoftware Thank you, I missed that. I’ll be using the default session configuration for my unit tests (Can’t unit-test background downloads anyway).

@nicolasmiari-unext Background sessions are handled by the system itself, we can’t hook on them as they are performed out-of-process and thus out of reach for interception. See the “Known Limitations” section in the README for more info.