【问题标题】:How do I simulate location for xctest如何模拟 xctest 的位置
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 您链接到的答案实际上建议使用 KVO 更新而不是实现 LocationManager 委托。但是我确实开始尝试您推荐的方法,并且 LocationManager 不断给我一个空位置。我改变了我的测试循环 - 查看编辑。
猜你喜欢
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
相关资源
最近更新 更多