我想为 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");
}