Fail-fast mocks incompatible with OCHamcrest assertions

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Fail-fast mocks incompatible with OCHamcrest assertions

Postby penfold » 20 Sep 2011, 08:02

There are some unfortunate unexpected method invocations when using the default fail-fast mocks together with OCHamcrest assertions:

1. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[Example]: unexpected method invoked: conformsToProtocol:<Protocol: 0x4c1b0c8> '
2. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[Example]: unexpected method invoked: isKindOfClass:<??>

I believe this is because OCHamcrest is attempting to describe the object in question and checks if it conforms to HCMatcher while evaluating is():
Code: Select all
id mock = [OCMockObject mockForClass:[Example class]];
ClassUnderTest *test = [ClassUnderTest classUnderTestWithExample:mock];
assertThat(test.example, is(mock));


For the time being I'm using niceMockForClass: to keep things simple but I was able to stub the methods as follows:
Code: Select all
BOOL no = NO;
[[[mock stub] andReturnValue:OCMOCK_VALUE(no)] conformsToProtocol:OCMOCK_ANY];
[[[mock stub] andReturnValue:OCMOCK_VALUE(no)] isKindOfClass:OCMOCK_ANY];


While this seems to work okay, I imagine it would be problematic if I were testing any code that makes use of protocols or type checking.

p.s. The combination of OCUnit/OCMock/OCHamcrest is fantastic... I feel like I'm writing JUnit/Mockito/Hamcrest. Thanks :-)
penfold
 

Return to OCMock



cron