我使用KIF框架(http://github.com/kif-framework/KIF http://github.com/kif-framework/KIF) 用于 UI 测试
我需要模拟位置服务。
问题是位置服务在 KIF 方法 -beforeAll 调用之前启动。
所以现在嘲笑已经太晚了。
任何建议,将不胜感激。
在我的 KIF 目标中,我有一个BaseKIFSearchTestCase : KIFTestCase
,我在类别中覆盖 CLLocationManager 的 startUpdatingLocation。
请注意,这是我做过的唯一一个类别覆盖,因为总的来说这确实不是一个好主意。但在测试目标中我可以接受。
#import <CoreLocation/CoreLocation.h>
#ifdef TARGET_IPHONE_SIMULATOR
@interface CLLocationManager (Simulator)
@end
@implementation CLLocationManager (Simulator)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(void)startUpdatingLocation
{
CLLocation *fakeLocation = [[CLLocation alloc] initWithLatitude:41.0096334 longitude:28.9651646];
[self.delegate locationManager:self didUpdateLocations:@[fakeLocation]];
}
#pragma clang diagnostic pop
@end
#endif // TARGET_IPHONE_SIMULATOR
#import "BaseKIFSearchTestCase.h"
@interface BaseKIFSearchTestCase ()
@end
@implementation BaseKIFSearchTestCase
//...
@end
Cleaner 将有一个子类CLLocationManager
在您的应用程序目标中,以及在测试目标中具有相同名称的另一个子类,该子类发送虚假位置,如上所示。但是这是否可能取决于您的测试目标的设置方式,因为它实际上需要是 Calabash 使用的应用程序目标。
还有另一种方式:
在您的项目中创建另一个配置“测试”,克隆“调试”
add the Preprocessor Macro
TESTING=1
到该配置。
子类CLLocationManager
使用您将使用 LocationManager 的子类
-
有条件地编译该类
#import "GELocationManager.h"
@implementation GELocationManager
-(void)startUpdatingLocation
{
#if TESTING==1
#warning Testmode
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CLLocation *fakeLocation = [[CLLocation alloc] initWithLatitude:41.0096334 longitude:28.9651646];
[self.delegate locationManager:self didUpdateLocations:@[fakeLocation]];
});
#else
[super startUpdatingLocation];
#endif
}
@end
在您的测试目标方案中选择新配置
还有另一个选择:
可能是最好的:不需要更改代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)