aspyct
aspyct

Reputation: 3875

OCMock 3 not matching the string argument

I'm starting to use OCMock (3.1.2) in my project for unit tests, and I'm having a weird behaviour. See the following test:

- (void)testOCMock
{
    OCMExpect([self.delegate openEmailComposer:@"[email protected]"]);

    [self.delegate openEmailComposer:@"hello there"];
}

Surprisingly, the test succeeds. Am I missing something?

Upvotes: 0

Views: 222

Answers (1)

Erik Doernenburg
Erik Doernenburg

Reputation: 3014

I guess what you are expecting was strict (or fail-fast) mode. In your example, were you expecting the mock to complain that openEmailComposer: with a @"hello there" argument was unexpected? In that case use a strict mock.

Normally the focus is on methods that should be called. The simple failure case is something like this:

OCMExpect([self.delegate openEmailComposer:@"[email protected]"]);
[self.delegate openEmailComposer:@"hello there"];
OCMVerifyAll(); // fails because openEmailComposer: wasn't called with the right argument

Last but not least, I would suggest to use the newer verify-after-running approach:

[self.delegate openEmailComposer:@"hello there"];
OCMVerify([self.delegate openEmailComposer:@"[email protected]"]); // fails

Upvotes: 2

Related Questions