3

我想为 TnSettings 做模拟,是的,如果通过以下方法编写代码,它可以工作,问题是我们需要为每个案例编写模拟代码,如果我们只模拟一次然后执行多个案例,那么第二个将报告异常。我使用最新的 OCMock V2.01。

我的问题是,为什么 OCMock 有这样的限制?还是我没有正确使用它?

任何想法或讨论将不胜感激,在此先感谢。

- (void) testFormattedDistanceValueWithMeters {         
        mockSettings = [OCMockObject mockForClass:[TnSettings class]];
        mockClientModel = [TnClientModel createMockClientModel];
        [[[mockClientModel expect] andReturn:mockSettings] settings];
        [[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];

        NSNumber *meters = [NSNumber numberWithDouble:0.9];
        distance = [NSString formattedDistanceValueWithMeters:meters];
        STAssertEqualObjects(distance, @"0.9", @"testformattedEndTimeForTimeInSeconds failed");

        //------------- Another case -----------------
        mockSettings = [OCMockObject mockForClass:[TnSettings class]];
        mockClientModel = [TnClientModel createMockClientModel];
        [[[mockClientModel expect] andReturn:mockSettings] settings];
        [[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];

        meters = [NSNumber numberWithDouble:100.9];
        distance = [NSString formattedDistanceValueWithMeters:meters];
        STAssertEqualObjects(distance, @"101", @"testformattedEndTimeForTimeInSeconds failed");
}
4

1 回答 1

5

不确定我是否完全理解您的问题或您的代码。不过,我怀疑您偶然发现了expectstub之间的区别。

这是你的想法吗?

- (void) testFormattedDistanceValueWithMeters {         
    mockSettings = [OCMockObject mockForClass:[TnSettings class]];
    mockClientModel = [TnClientModel createMockClientModel];
    [[[mockClientModel stub] andReturn:mockSettings] settings];
    [[[mockSettings stub] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];

    NSNumber *meters = [NSNumber numberWithDouble:0.9];
    distance = [NSString formattedDistanceValueWithMeters:meters];
    STAssertEqualObjects(distance, @"0.9", @"testformattedEndTimeForTimeInSeconds failed");

    meters = [NSNumber numberWithDouble:100.9];
    distance = [NSString formattedDistanceValueWithMeters:meters];
    STAssertEqualObjects(distance, @"101", @"testformattedEndTimeForTimeInSeconds failed");

}

于 2012-07-03T15:40:58.290 回答