Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use OCMock to verify that a method is never called?

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

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!"); } 
like image 605
Justin Voss Avatar asked Apr 29 '10 16:04

Justin Voss


1 Answers

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

Quoted from: http://www.mulle-kybernetik.com/software/OCMock/#features

like image 135
InsertWittyName Avatar answered Oct 01 '22 04:10

InsertWittyName