如何使用 OCMock 验证方法调用的数量
有没有办法验证一个方法是否已被调用“x”次?
Is there a way to verify that a method has been called 'x' amount of times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法验证一个方法是否已被调用“x”次?
Is there a way to verify that a method has been called 'x' amount of times?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
查看 OCMock 的测试文件,似乎您需要拥有与呼叫数量相同的
expect
数量。因此,如果你调用someMethod
三次,你需要做...虽然这看起来很难看,也许你可以把它们放在一个循环中?
Looking at the test file for OCMock, it seems that you need to have the same number of
expect
s as you have calls. So if you callsomeMethod
three times, you need to do...This seems ugly though, maybe you can put them in a loop?
我通过利用委托给块的能力取得了成功:
在块内,我只需增加一个
callCount
变量,然后断言它与预期的调用数量匹配。例如:每次调用
someMethod
时都应调用该块,因此callCount
应始终与实际调用该方法的次数相同。I've had success by leveraging the ability to delegate to a block:
Inside the block, I just increment a
callCount
variable, and then assert that it matches the expected number of calls. For example:The block should be invoked each time
someMethod
is called, socallCount
should always be the same as the number of times the method was actually called.如果您需要检查一个方法是否只被调用一次,您可以这样做
If you need to check if a method is only called once, you can do it like this