gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:08:43 25 4
gpt4 key购买 nike

在我的日常工作中,我被 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com