如何模拟xctest的位置

mki*_*irk 7 testing google-maps core-location ios

我正在尝试编写一些单元测试(使用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);

}
Run Code Online (Sandbox Code Playgroud)

以下是分配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];
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

以下是我到目前为止所得到的:通过编辑Xcode中的方案首选项,我可以在构建应用程序时模拟位置.

编辑位置模拟的屏幕截图

然后,当我从xcode构建应用程序时,我可以在模拟器中看到我的位置已更新为"旧金山".这是我在模拟器上构建和运行时日志的输出:

2014-04-02 17:28:09.253 DEBUG WtHomeController: observeValueForKeyPath:ofObject:change:context: 109 - my location is: 37.787359, -122.408227
Run Code Online (Sandbox Code Playgroud)

凉.但是在运行单元测试时,我的位置永远不会更新.

测试日志的输出是:

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
Run Code Online (Sandbox Code Playgroud)

如何模拟单元测试的位置?

根据@ 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]);
  }
Run Code Online (Sandbox Code Playgroud)

并且运行该测试的新日志输出:

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)
Run Code Online (Sandbox Code Playgroud)

Pau*_*w11 -2

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

我认为您没有为 CLLocationManager 设置委托方法 - 它是didUpdateLocations将位置传输到 Google 地图视图的委托方法

viewDidLoad

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

实现委托方法(请注意,上一个答案中提到的方法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

}
Run Code Online (Sandbox Code Playgroud)

请参阅CLLocationManager 参考