我正在写一些测试,我需要

  • 存根对模拟CLLocationManager的调用以返回特定的
    CLLocation
  • ,然后CLLocation需要具有一个时间戳
    过去是

  • 创建CLLocation的实例很容易,但是它的timestamp属性是只读的,并且固定为创建实例的时间点。
    因此,我计划创建一个模拟CLLocation,并同时对时间戳调用进行存根。

    因此,代码如下所示:
    [[CLLocationManager stubAndReturn:theValue(YES)] locationServicesEnabled];
    NSDate *oldDate = [IPPOTestSupportMethods createNSDateSubtractingDays:2];
    //TODO - Why is line below failing
    [[expectedOldLocationMock stubAndReturn:oldDate] timestamp];
    [[locationMgrMock stubAndReturn:expectedOldLocationMock] location];
    

    总而言之,我有一个CLLocationManager模拟,我创建了一个比今天早两天的NSDate。我希望在致电时返回该日期
    [cllocationmock timestamp];
    

    但是,我正在和ARC语义问题。
    IPPOLocationManagerDelegateImplKiwiTests.m:203:33: Multiple methods named 'timestamp' found with mismatched result, parameter type or attributes
    

    这是猕猴桃的问题,还是我错过了什么?

    最佳答案

    我能够通过使用选择器-插入技术而不是消息模式技术来使这项工作:

    [expectedOldLocationMock stub:@selector(timestamp) andReturn:oldDate];
    

    使用消息模式技术(stubAndReturn:)时,出现与您相同的错误:

    发现多个名为“时间戳记”的方法,它们的结果,参数类型或属性不匹配

    如果在“问题导航器”中检查此错误,则应看到它指向两个声明了timestamp选择器的不同类:UIAcceleration类声明
    @property(nonatomic,readonly) NSTimeInterval timestamp;
    

    …类CLLocation声明
    @property(readonly, nonatomic) NSDate *timestamp;
    

    请注意“消息模式”存根技术的结构:
    [[someObject stubAndReturn:expectedValue] messageToStub]
    
    stubAndReturn:方法返回类型为id的不透明对象。因此,这等效于:
    id invocationCapturer = [someObject stubAndReturn:expectedValue];
    [invocationCapturer messageToStub];
    

    在这里,“messageToStub”是您的timestamp选择器。因此,您要说的是将消息timestamp发送到id类型的未知对象。由于在编译时我们没有断言要发送的对象的类型为时间戳选择器,因此它无法知道您所指的那个时间戳属性的版本,因此无法确定正确的返回类型。

    您只需执行以下操作即可重现相同的错误:
    id someUnknownObject;
    [someUnknownObject timestamp];
    

    结论是,当存在相同选择器名称的不同声明时,消息模式存根技术将无法很好地工作。

    关于ios - 无法在Kiwi中的模拟CLLocation对象上添加时间戳记,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18042900/

    10-11 17:09