在我的單元測試,我爲了現在用的-[XCTestCase keyValueObservingExpectationForObject:keyPath:handler:]
方法來確保我的NSOperation完成,這裏是code from my XCDYouTubeKit project:XCTest異常時keyValueObservingExpectationForObject:的keyPath:處理器:
- (void) testStartingOnBackgroundThread
{
XCDYouTubeVideoOperation *operation = [[XCDYouTubeVideoOperation alloc] initWithVideoIdentifier:nil languageIdentifier:nil];
[self keyValueObservingExpectationForObject:operation keyPath:@"isFinished" handler:^BOOL(id observedObject, NSDictionary *change)
{
XCTAssertNil([observedObject video]);
XCTAssertNotNil([observedObject error]);
return YES;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
XCTAssertFalse([NSThread isMainThread]);
[operation start];
});
[self waitForExpectationsWithTimeout:5 handler:nil];
}
這個測試總是通過時我這個錯誤本地運行我的Mac上,但有時它fails on Travis:
failed: caught "NSRangeException", "Cannot remove an observer <_XCKVOExpectation 0x1001846c0> for the key path "isFinished" from <XCDYouTubeVideoOperation 0x1001b9510> because it is not registered as an observer."
難道我做錯了什麼?
@Cœur[一般共識](https://meta.stackoverflow.com/questions/274906/should-questions-that-violate-api-terms-of-service-be-flagged)是它不是stackoverflow用戶或版主執行其他網站的ToS的責任。 –