Justin Voss
Justin Voss

Reputation: 6384

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!");
}

Upvotes: 37

Views: 18176

Answers (6)

Tai Le
Tai Le

Reputation: 9296

To make sure your method not called, I think we have to do an assert before the testMethod called. So make sure put OCMReject before you run the test method to listen which method will trigger when runs testMethod:

OCMReject([mock someMethod]);
[mock testMethod];

Upvotes: 0

Ruslan Mansurov
Ruslan Mansurov

Reputation: 1301

Starting from version 3.3 OCMock has OCMReject macro.

    id mock = OCMClassMock([MyObject class]);
    OCMReject([mock forbiddenMethod]);

    // exception will raise
    [mock forbiddenMethod];

Upvotes: 10

Ben Flynn
Ben Flynn

Reputation: 18932

You may also find it necessary to ensure a method is never being called on an object you are partially mocking.

I created a macro for this:

#define andDoFail andDo:^(NSInvocation *invocation) { STFail(@"Should not have called this method!"); }

I use it like this:

[[[_myPartialMock stub] andDoFail] unexpectedMethod];

Upvotes: 3

InsertWittyName
InsertWittyName

Reputation: 3940

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

Upvotes: 65

user224851
user224851

Reputation:

You could also try something like this, a la JavaScript:

- (void)aMethod {
   __block BOOL b = NO;

   id mock = [OCMockObject mockForClass:[UIView class]];
   [[[mock stub] andDo:^(NSInvocation *i) { b = YES; }] resignFirstResponder];
   [mock resignFirstResponder];

   NSLog(@"And b is: %i", b); // This reads "And b is: 1" on the console
}

I'm not sure if there are any leaks associated with this code. I got the idea from reading this page: http://thirdcog.eu/pwcblocks/

Upvotes: 0

Johannes Rudolph
Johannes Rudolph

Reputation: 35761

As far as I know OCMock will fail automatically when you call verify and methods that have not been recorded were called. A mock that wouldn't complain if unexpected methods were called is called a "nice mock".

- (void)testSomeMethodIsNeverCalled {
    id mock = [OCMockObject mockForClass:[MyObject class]];

    [mock forbiddenMethod];
    [mock verify]; //should fail
}

Upvotes: 19

Related Questions