如何使用KIF框架模拟位置服务

n0_*_*ter 6 automated-tests objective-c ui-automation ios kif

我使用KIF框架(http://github.com/kif-framework/KIF)进行UI测试,我需要模拟位置服务.

问题是位置服务启动BEFORE KIF方法-beforeAll调用.因此,嘲笑为时已晚.

任何建议,将不胜感激.

Vik*_*ica 3

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

更干净的方法是CLLocationManager在您的应用程序目标中拥有一个 的子类,并在您的测试目标中拥有另一个同名的子类,该子类发送虚假位置,如上所示。但是这是否可能取决于您的测试目标的设置方式,因为它实际上需要是 Calabash 使用的应用程序目标。


还有另一种方式:

  • 在您的项目中创建另一个配置“测试”,克隆“调试”

  • 将 添加Preprocessor Macro TESTING=1到该配置中。

  • 子类CLLocationManager

  • 在需要使用 CLLocaltionManger 的地方使用该子类

  • 有条件地编译该类

    #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
    
    Run Code Online (Sandbox Code Playgroud)
  • 在您的测试目标方案中选择新配置


还有另一个选择:

在此输入图像描述

可能是最好的:不需要更改代码。