Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 442
fabian789 Avatar asked Jul 11 '11 17:07

fabian789


1 Answers

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

like image 84
Sven Avatar answered Sep 19 '22 10:09

Sven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!