如何使用OCMock来验证方法从未被调用? [英] How can I use OCMock to verify that a method is never called?

查看:618
本文介绍了如何使用OCMock来验证方法从未被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一天工作,我被宠坏了 Mockito的 never()验证,可以确认模拟方法从未被调用。

At my day job I've been spoiled with Mockito's never() verification, which can confirm that a mock method is never called.

有没有办法使用Objective-C和OCMock完成同样的事情?我一直在使用下面的代码,这是工作,但它感觉像一个黑客。我希望有更好的方法...

Is there some way to accomplish the same thing using Objective-C and OCMock? I've been using the code below, which works but it feels like a hack. I'm hoping there's a better way...

- (void)testSomeMethodIsNeverCalled {
    id mock = [OCMockObject mockForClass:[MyObject class]];
    [[[mock stub] andCall:@selector(fail) onObject:self] forbiddenMethod];

    // more test things here, which hopefully
    // never call [mock forbiddenMethod]...
}

- (void)fail {
    STFail(@"This method is forbidden!");
}


推荐答案

自OCMock r69以来,可以拒绝方法调用 http://svn.mulle-kybernetik.com/OCMock/trunk/ Source / Changes.txt

Since r69 of OCMock, it's possible to reject a method call http://svn.mulle-kybernetik.com/OCMock/trunk/Source/Changes.txt


Nice mocks / failing fast当一个
方法被调用一个mock对象
没有设置与期望
或stub mock对象将引发一个
异常。通过创建一个漂亮模拟,可以关闭这个失败模式:

Nice mocks / failing fast When a method is called on a mock object that has not been set up with either expect or stub the mock object will raise an exception. This fail-fast mode can be turned off by creating a "nice" mock:

id mock = [OCMockObject niceMockForClass:[SomeClass class]]

虽然好的mock会忽略
所有意想不到的方法可能
禁止具体方法:

While nice mocks will simply ignore all unexpected methods it is possible to disallow specific methods:

[[mock reject] someMethod]

请注意,在故障快速模式下,如果忽略
异常,将会验证
rethrown称为。这个
可以确保可以检测到
不需要的来自
通知等的调用。

Note that in fail-fast mode, if the exception is ignored, it will be rethrown when verify is called. This makes it possible to ensure that unwanted invocations from notifications etc. can be detected.

引述自: http://www.mulle-kybernetik.com/software/OCMock/#features

这篇关于如何使用OCMock来验证方法从未被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆