Trong các thử nghiệm đơn vị của tôi, tôi đang sử dụng phương pháp -[XCTestCase keyValueObservingExpectationForObject:keyPath:handler:]
để đảm bảo rằng NSOperation của tôi kết thúc, đây là code from my XCDYouTubeKit project:ngoại lệ XCTest khi sử dụng keyValueObservingExpectationForObject: keyPath: handler:
- (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];
}
thử nghiệm này luôn đi khi tôi chạy nó cục bộ trên máy Mac của tôi nhưng đôi khi nó fails on Travis với lỗi này:
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."
tôi có làm điều gì sai?
@ Cœur [Sự đồng thuận chung] (https://meta.stackoverflow.com/questions/274906/should-questions-that-violate-api-terms-of-service-be-flagged) là nó không phải là trách nhiệm của người dùng stackoverflow hoặc người kiểm duyệt để thực thi các ToS của các trang web khác. –