如何使用KIF框架嘲笑位置服务 [英] How to mock location service using KIF-framework

查看:268
本文介绍了如何使用KIF框架嘲笑位置服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的UI测试KIF框架( http://github.com/kif-framework/KIF
我需要模拟定位服务。

I use KIF framework (http://github.com/kif-framework/KIF) for UI Tests and I need to mock location service.

问题是位置服务开始之前KIF方法-beforeAll调用。
所以这是为时已晚来嘲笑。

The problem is location service starts BEFORE KIF method -beforeAll invoked. So it's too late to mock.

任何建议将是AP preciated。

Any suggestions would be appreciated.

推荐答案

在我KIF目标我有一个 BaseKIFSearchTestCase:KIFTestCase ,我在那里覆盖CLLocationManager`s startUpdatingLocation在类别。

In my KIF target I have a BaseKIFSearchTestCase : KIFTestCase, where I overwrite CLLocationManager`s startUpdatingLocation in a category.

请注意,这是我曾经因为这样做的唯一类别覆盖真的不是一般的好主意。但在测试目标,我可以接受。

Note that this is the only category overwrite I ever made as this is really not a good idea in general. but in a test target I can accept it.

#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


清洁将有 CLLocationManager 的应用目标一个子类,并在你的测试目标是发送伪造的位置,就像上面显示的另一个同名的子类。但是,如果这是可能取决于你的测试目标是如何设置的,因为它实际上需要一个应用程序的目标是葫芦使用它。


Cleaner would be to have a subclass of CLLocationManager in your application target and another subclass with the same name in your test target that send fake location like shown above. But if this is possible depends on how your test target is set up, as it actually need to be an application target as Calabash uses it.

另一种方式:


  • 在项目中创建另一个配置测试克隆调试

  • in your project create another configuration "Testing", cloning "Debug"

添加 preprocessor宏 检测= 1 该配置。

子类 CLLocationManager

使用该子类,你会使用CLLocaltionManger

use that subclass where you would use 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


  • 在您的测试目标,方案选择新的配置。

  • in your test targets scheme choose the new configuration

    和另一种选择:

    在这里输入的形象描述

    也许是最好的:没有code需要修改

    Probably the best: no code needs to be changed.

    这篇关于如何使用KIF框架嘲笑位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆