W moich testów jednostkowych, używam metody -[XCTestCase keyValueObservingExpectationForObject:keyPath:handler:]
w celu zapewnienia, że moja NSOperation kończy, tutaj jest code from my XCDYouTubeKit project:XCTest wyjątek podczas korzystania keyValueObservingExpectationForObject: keypath: obsługi:
- (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];
}
Ten test zawsze przechodzi kiedy I uruchomić go lokalnie na moim Mac, ale czasami fails on Travis z tego błędu:
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."
Czy robię coś źle?
@ Cœur [Ogólny konsensus] (https://meta.stackoverflow.com/questions/274906/should-questions-that-violate-api-terms-of-service-be-flagged) jest taki, że nie jest odpowiedzialność stackoverflow użytkowników lub moderatorów w celu egzekwowania ToS innych stron internetowych. –