在我的单元测试,我为了现在用的-[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的责任。 –