首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OCMockito获取模拟调用的计数

OCMockito获取模拟调用的计数
EN

Stack Overflow用户
提问于 2016-09-02 17:05:39
回答 1查看 87关注 0票数 0

如何获得对模拟对象的调用计数?

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

这应该是这样的:

代码语言:javascript
复制
[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];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-03 16:48:10

你可以用块来嘲笑任何东西。所以让我们用一个块来增加我们自己的计数器。

代码语言:javascript
复制
__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))));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39297544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档