【发布时间】:2014-05-14 13:28:06
【问题描述】:
我正在尝试编写一些单元测试(使用 xctest)来验证我的 Google 地图 mapView 是否以我的“当前位置”为中心。
- (void) testMapCentersOnMyLocation {
WtHomeController *controller = [[WtHomeController alloc] init];
[controller viewDidLoad];
//Simulator's location is stubbed to be in San Francisco, at this lat/lon
CLLocationDegrees expectedLatitude = 37.787359f;
CLLocationDegrees expectedLongitude = -122.408227f;
// FIXME this loop runs forever - location isn't being updated in simulator.
while(controller.firstLocationUpdate == NO) {
NSLog(@"Waiting on location...");
NSLog(@"my location is: %f, %f", controller.mapView.myLocation.coordinate.latitude, controller.mapView.myLocation.coordinate.longitude);
}
CLLocationCoordinate2D mapCenter = [controller.mapView.camera target];
XCTAssertEqualWithAccuracy(mapCenter.latitude, expectedLatitude, 0.00001f);
XCTAssertEqualWithAccuracy(mapCenter.longitude, expectedLongitude, 0.00001f);
}
以下是分配 mapView 的控制器的相关方法
@interface WtHomeController ()
@property(nonatomic, strong) GMSMapView *mapView;
@property(nonatomic) BOOL firstLocationUpdate;
@end
@implementation WtHomeController
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize map pane on Los Angeles lat/lon
CLLocationDegrees latitude = WtLosAngelesLatitude;
CLLocationDegrees longitude = WtLosAngelesLongitude;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:6];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
// Listen to the myLocation property of GMSMapView.
[self.mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = self.mapView;
}
- (void)dealloc {
[self.mapView removeObserver:self
forKeyPath:@"myLocation"
context:NULL];
}
#pragma mark - KVO updates
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
DDLogDebug(@"my location is: %f, %f", self.mapView.myLocation.coordinate.latitude, self.mapView.myLocation.coordinate.longitude);
if( [keyPath isEqualToString:@"myLocation"] ) {
if (!self.firstLocationUpdate) {
// If the first location update has not yet been recieved, then jump to that
// location.
self.firstLocationUpdate = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
self.mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
}
到目前为止,我的想法是:我可以在构建应用时通过在 Xcode 中编辑方案首选项来模拟位置。
然后,当我从 xcode 构建应用程序时,我可以在模拟器中看到我的位置已更新为“旧金山”。这是我在模拟器上构建和运行时的日志输出:
2014-04-02 17:28:09.253 DEBUG WtHomeController: observeValueForKeyPath:ofObject:change:context: 109 - my location is: 37.787359, -122.408227
酷。但是在运行单元测试时,我的位置永远不会更新。
测试日志的输出是:
2014-04-02 17:02:41.919 wetap[71081:60b] Waiting on location...
2014-04-02 17:02:41.919 wetap[71081:60b] my location is: 0.000000, 0.000000
2014-04-02 17:02:41.919 wetap[71081:60b] Waiting on location...
2014-04-02 17:02:41.920 wetap[71081:60b] my location is: 0.000000, 0.000000
2014-04-02 17:02:41.920 wetap[71081:60b] Waiting on location...
2014-04-02 17:02:41.921 wetap[71081:60b] my location is: 0.000000, 0.000000
2014-04-02 17:02:41.921 wetap[71081:60b] Waiting on location...
2014-04-02 17:02:41.921 wetap[71081:60b] my location is: 0.000000, 0.000000
...to infinity
如何模拟单元测试的位置?
根据@Paulw11 的建议编辑了我的测试:
WtAppDelegate *appdelegate = (WtAppDelegate *)[UIApplication sharedApplication].delegate;
CLLocationManager *locationManager = appdelegate.locationManager;
[locationManager startUpdatingLocation];
while(controller.firstLocationUpdate == NO) {
NSLog(@"Waiting on location...");
NSLog(@"my location is: %f, %f", controller.mapView.myLocation.coordinate.latitude, controller.mapView.myLocation.coordinate.longitude);
NSLog(@"Location manager think's we're at: %@", [locationManager location]);
}
以及运行该测试的新日志输出:
2014-04-02 18:08:56.394 wetap[73922:60b] Location manager think's we're at: (null)
2014-04-02 18:08:56.394 wetap[73922:60b] Waiting on location...
2014-04-02 18:08:56.394 wetap[73922:60b] my location is: 0.000000, 0.000000
2014-04-02 18:08:56.395 wetap[73922:60b] Location manager think's we're at: (null)
2014-04-02 18:08:56.396 wetap[73922:60b] Waiting on location...
2014-04-02 18:08:56.396 wetap[73922:60b] my location is: 0.000000, 0.000000
2014-04-02 18:08:56.397 wetap[73922:60b] Location manager think's we're at: (null)
【问题讨论】:
-
你能显示你分配了 CLLocationManager 并启用了位置更新的代码吗?
-
当我手动运行模拟器时,我已经包含了相关的(我认为)控制器代码以及成功的日志输出。 @Paulw11
标签: ios google-maps testing core-location