objective-c - 我如何使用 OCMock 来验证一个方法从未被调用过?

标签 objective-c cocoa mocking mockito ocmock

在我的日常工作中,我被 Mockito's never() verification 宠坏了,这可以确认从未调用模拟方法。

有没有什么方法可以使用 Objective-C 和 OCMock 来完成同样的事情?我一直在使用下面的代码,它可以工作,但感觉像是 hack。我希望有更好的方法...

- (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

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]]

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

[[mock reject] someMethod]

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

关于objective-c - 我如何使用 OCMock 来验证一个方法从未被调用过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2739130/

相关文章:

ios - Xcode 12 模拟器崩溃

ios - AFNetworking 3.0 错误 : “Request failed: internal server error (500)”

c# - 使用 MonoMac 实现 NSOutlineViewDataSource

java - 使用 'any' 使用 JMockit stub 静态方法

c# - 我可以使用 FakeItEasy 伪造 Properties.Settings.Default 属性吗?

ios - xcode 选项卡栏 Controller 使用代码更改所选项目

iphone - NSMutablearray 将对象从索引移动到索引

iphone - iOS 通知中的本地化?

objective-c - NSTableView 和 NSMutableArray 之间的 Cocoa 绑定(bind)拒绝更新

ruby-on-rails - RSpec - 模拟类方法