如何获取模拟对象的调用次数?
在测试的某个特定点,我想获取某个方法的当前调用计数,然后继续测试并最终验证该方法是否被再次调用。
这会是这样的:
[given([mockA interestingMethod]) willReturnInt:5];
<do some work that may call 'interestingMethod' one or two times>
NSInteger count = currentCountOfInvocations([mockA interestingMethod]); //or something similar
<do some more work that [hopefully] calls interesting method one more time>
[verifyCount(mockA, times(count + 1)) interestingMethod];
最佳答案
你可以用一个 block 模拟任何东西。因此,让我们使用一个 block 来增加我们自己的计数器。
__block NSUInteger interestingMethodCount = 0;
[given([mockA interestingMethod]) willDo:^id(NSInvocation *invocation) {
interestingMethodCount += 1;
return @5;
}];
<do some work that may call 'interestingMethod' one or two times>
NSUInteger countAtCheckpoint = interestingMethodCount;
<do some more work that [hopefully] calls 'interestingMethod' one more time>
assertThat(@(interestingMethodCount), is(equalTo(@(countAtCheckpoint + 1))));
关于ios - OCMockito 获取模拟调用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297544/