问题描述
在我的一天工作,我被宠坏了,可以确认模拟方法从未被调用。
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以来,可以拒绝方法调用
Since r69 of OCMock, it's possible to reject a method call http://svn.mulle-kybernetik.com/OCMock/trunk/Source/Changes.txt
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.
引述自:
这篇关于如何使用OCMock来验证方法从未被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!