gpt4 book ai didi

ios - 我如何模拟 xctest 的位置

转载 作者:可可西里 更新时间:2023-11-01 03:35:21 24 4
gpt4 key购买 nike

我正在尝试编写一些单元测试(使用 xctest)来验证我的 Google map 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);

}

下面是 Controller 分配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)

最佳答案

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

我认为您没有为 CLLocationManager 设置委托(delegate)方法 - 是 didUpdateLocations 委托(delegate)方法将位置传输到 Google map View

viewDidLoad

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

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

关于ios - 我如何模拟 xctest 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22825427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com