ios - OCMockito 获取模拟调用计数

标签 ios unit-testing ocmockito

如何获取模拟对象的调用次数?

在测试的某个特定点,我想获取某个方法的当前调用计数,然后继续测试并最终验证该方法是否被再次调用。

这会是这样的:

[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/

相关文章:

javascript - 使用 Mocha 测试 Promise

ios - 使用 OCMockito 测试 Storyboard的最佳方式

ios - 使用 OCMockito 如何验证任何选择器是否已传递给方法

ios - OCMockito 验证任何参数

ios - 具有 queryEqual 值的 observeSingleEvent 不起作用

c++ - 对以副作用为目的的函数进行单元测试

ios - 如何过滤时间戳数组以保持特定时间?

java - 在 JUnit 测试期间禁用部分 Java 程序

ios - 如何更改 UILabel 中字符类型(而非字符串)的颜色?

iOS 11 : UISearchController has a strange space in between UITableView and UISearchBar