在尝试将 TDD 应用于异步代码时,我发现在部署目标中工作的相同代码在测试目标中不起作用。我使用 CLLocationManager 发现的这个问题的一个例子:
- (void)testReceivingLocation
{
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
locationManager.pausesLocationUpdatesAutomatically = NO;
if ([CLLocationManager locationServicesEnabled])
{
[locationManager startUpdatingLocation];
}
startLocation = nil;
NSDate *until = [NSDate dateWithTimeIntervalSinceNow:10];
while ([until timeIntervalSinceNow] > 0)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:until];
}
XCTAssert(alreadyReceivedLocation, @"Location wasn't received.");
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
alreadyReceivedLocation = true;
// Never actually get there.
}
可能是什么问题?