1

I'm attempting to unit test the networking code in my app, and I'm having a few issues where I want to see my test fail to ensure it's all working, before I go and get it all passing. The thing is, it's always passing. I'm using Xcode6-Beta7 in case that could be the problem

I'm using Specta for the tests, and using OHHTTPStubs to stub my network request and to return a status code in the response.

Here's the code for my set up and tear down

beforeEach(^{
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES;
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithData:nil statusCode:200 headers:nil];
    }].name = @"My Test Stub";

    [OHHTTPStubs onStubActivation:^(NSURLRequest *request, id<OHHTTPStubsDescriptor> stub) {
        NSLog(@"%@ stubbed by %@", request.URL, stub.name);
    }];
});

afterAll(^{
    [OHHTTPStubs removeAllStubs];
});

In the next part of the test (the problematic area), I'm creating an instance of my class that actions a NSMutableURLRequest and then checking if that isn't nil.

context(@"Given that I create a http request with a URL", ^{
    __block NLXApiBaseServices *baseService = [[NLXApiBaseServices alloc] init];
    NSMutableURLRequest *testRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];

    it(@"should have a base service created", ^{
        expect(baseService).toNot.beNil();
    });

This first test works, then the next test uses the stub. This should fail, as my stub should return a 200, and I'm testing for it to be equal to 400

    it(@"should have a return code of 200", ^AsyncBlock{
        [baseService dataWithHttpRequest:testRequest successCallback:^(NSInteger statusCode, NSDictionary *response, NSDictionary *responseHeaders) {
            expect(statusCode).to.equal(400);
            done();
        } errorCallback:^(NSInteger statusCode, NSDictionary *response, NSError *error) {
        }];
    });

This is where I perform an asynchronous test using the AsyncBlock keyword, and using done() to signal that it's complete. What I'm noticing is that Xcode reports back that the tests succeeded, but looking at the logging in my console I can see

Test Suite 'All tests' passed at 2014-09-07 22:11:22 +0000. Executed 95 tests, with 0 failures (0 unexpected) in 0.489 (0.816) seconds

On close inspection of my logging, I've found

2014-09-07 23:11:21.480 TwoUpTop[9255:116602] Request <http://www.google.com>
2014-09-07 23:11:21.486 TwoUpTop[9255:116661] http://www.google.com stubbed by My Test Stub
2014-09-07 23:11:21.487 TwoUpTop[9255:116648] NLXApiBaseServicesSpec.m:49 expected: 400, got: 200
Test Case '-[NLXApiBaseServicesSpec   NLXApiBaseServices_Given_that_I_create_a_http_request_with_a_URL_should_have_a_return_code_of_200]' passed (0.012 seconds).

So now, I'm confused - it's complaining that the status codes aren't equal - but the tests are passing!!!

Does anybody out there have any idea if I'm doing something wrong?

In case it helps, the NLXApiBaseService class creates a NSURLSessionDataTask from a NSURLSessionConfiguration object, which is created when I initialise the NLXApiBaseService class, using a defaultSessionConfiguration.

1 Answers1

0

It would seem that this does the trick..

[baseService dataWithHttpRequest:testRequest successCallback:^(NSInteger statusCode, NSDictionary *response, NSDictionary *responseHeaders) 
      dispatch_async(dispatch_get_main_queue(), ^{
          expect(statusCode).to.equal(200);
      });
      done();
      } errorCallback:^(NSInteger statusCode, NSDictionary *response, NSError *error) {
}];

This also correctly reports failures when I test expect(statusCode).to.equal(404);

This was reported on Specta's Github page :- https://github.com/specta/specta/issues/44