Search code examples
iphoneiosunit-testingnsoperationnsoperationqueue

Unit Test NSOperation?


I would like to test an NSOperation subclass. I tried to do this in my SenTestCase subclass:

- (void)setUp {
    [super setUp];

    _importQueue = [[NSOperationQueue alloc] init];

    [_importQueue setMaxConcurrentOperationCount:1];
    [_importQueue waitUntilAllOperationsAreFinished];
}

- (void)tearDown {
    [_importQueue release];

    [super tearDown];
}

- (void)testSomeImport {
    ImportOperation *op = [[ImportOperation alloc] initWithFile:...];
    [_importQueue addOperation:op];
    [op setDelegate:self];
    [op release];
}

- (void)opDidFinish:(ImportOperation *)op {     // ImportOperation delegate method
    // Not getting called
}

But the tests finishes before the NSOperation finished executing, despite specifying waitUntilAllOperationsAreFinished.

Any ideas of how to prevent the test from finishing before my operation completed?


Solution

  • You need to call waitUntilAllOperationsAreFinished after you added the operation to the queue, not in setUp.