简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM