gpt4 book ai didi

OCMock只生效一次,奇怪,为什么?或者我这边有什么问题?

转载 作者:行者123 更新时间:2023-12-01 11:51:14 27 4
gpt4 key购买 nike

我想为 TnSettings 做 mock,是的,如果通过以下方法编写代码,它就可以工作,问题是我们需要为每个案例编写 mock 代码,如果我们只 mock 一次然后执行多个案例,那么第二个将报告异常。我用的是最新的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");
}

最佳答案

不确定我是否完全理解您的问题或您的代码。不过,我怀疑您无意中发现了 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");

关于OCMock只生效一次,奇怪,为什么?或者我这边有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com