0

I'm trying to test a class that has a method responding asynchronously that makes a network call, stubbed by Nocilla.

The tests runs fine when I run the test alone. But as soon as I launch my whole test suite, it blocks here for a while and finishes with a:

Thread 1: signal SIGABRT

Here is my test class:

@interface SMIMyServiceTests : XCTestCase

@property (strong, nonatomic) SMIMyService *service;

@end


@implementation SMIMyServiceTests

+ (void)setUp {
    [[LSNocilla sharedInstance] start];
}

+ (void)tearDown {
    [[LSNocilla sharedInstance] stop];
}

- (void)setUp {
    [super setUp];

    self.service = [[SMIMyService alloc] init];
}

- (void)tearDown {
    [[LSNocilla sharedInstance] clearStubs];
    self.service = nil;
    [super tearDown];
}

- (void)testFetch {
    stubRequest(@"GET", @"http://mydevserver.192.168.1.15.xip.io/api/data.json").andReturn(200).withBody([MyUtil jsonFromFile:@"json-file" sender:self]);

    XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch"];

    [self.service fetch:^(NSArray *data) {
        XCTAssertTrue(data != nil);
        XCTAssertEqual(data.count, 7);
        [expectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:5.0 handler:nil];
}

@end

Any idea what's going wrong?

Mick F
  • 7,312
  • 6
  • 51
  • 98
  • I would suggest abandoning your use of the class methods `+ (void)setUp` and `+ (void)tearDown`. You don't know what state any previous test leaves your `LSNocilla sharedInstance` in. Instead, start and stop the shared instance in the instance methods `- (void) setUp` and `- (void)tearDown`. Yes this may take longer but it obeys FIRST, and will thus help ensure that circumstances are the same for this test regardless whether it is run on its own or as one of a larger suite. – matt Mar 05 '15 at 16:00
  • Thanks @matt. Moving the `LSNocilla` code to the `-` methods didn't change the behavior though. – Mick F Mar 09 '15 at 16:51

0 Answers0