如何模拟xctest的位置

时间:2014-04-03 00:06:46

标签: ios google-maps testing core-location

我正在尝试编写一些单元测试(使用xctest)来验证我的Google Maps 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中的方案偏好来构建应用程序时模拟位置。

Screenshot of editing Location Simulation

然后,当我从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)

1 个答案:

答案 0 :(得分:-2)

Google地图视图只是一个视图,它不会自动连接到iOS中的位置跟踪并自行更新。您需要分配CLLocationManager并使用它的委托方法跟踪您的位置。然后,您可以在此答案中更新myLocation属性 - GMSMapView myLocation not giving actual location

我认为您没有为CLLocationManager设置委托方法 - didUpdateLocations委托方法会将位置转移到Google地图视图

viewDidLoad

中的

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
[self.locationManager startUpdatingLocation];

实现委托方法(请注意,上一个答案中引用的方法didUpdateLocation已被弃用并替换为didUpdateLocations方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *latestLocation=(CLLocation *)[locations lastObject];

    [self.mapView animateToLocation:latestLocation.coordinate]  <== this is the important bit - it transfers the location from CLLocationManager to the GM view

}

请参阅CLLocationManager reference