0
嘗試將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.
}
可以採取什麼問題嗎?
感謝指針!我用我使用的具體方法更新了問題,並嘗試使用您的代碼而不是sleep()。不幸的是,沒有任何改變... – Sergey
啊,CLLoactionManager我敢打賭,模擬器中沒有任何位置更新。你可以嘗試在GPS定位設備上運行測試嗎? CLLocationManager在模擬器中的行爲有點不同。 – sofacoder