Search code examples
iostddocunitocmock

OCMock throwing NSInternalInconsistencyException when parameters are not the ones expected


I'm setting up a mock object for a delegate object, to check that when the URL is nil, the delegate method is called with nil as parameters.

When the FileDownloadOperation behave as expected,the test pass, which is fine.

When the FileDownloadOperation doesn't call the delegate method, the test fails as expected.

But when the FileDownloadOperation calls the delegate method with something else than nil, instead of failing, the test crashes and no other tests are executed because OCMock throws :

'NSInternalInconsistencyException' reason: 'OCMockObject[FileDownloadOperationTest]: unexpected method invoked: data:<> forURL:nil

  -(void) testNilURL{
      // 1. Create an operation
      FileDownloadOperation * anOp = [[FileDownloadOperation alloc]init];
      // 2. set a nil URL
      anOp.URL = nil;
      // 3. set a mock delegate
      id mockDelegate = [OCMockObject mockForClass:[self class]];
      [[mockDelegate expect] data:[OCMArg isNil] forURL:[OCMArg isNil]];
      anOp.delegate = mockDelegate;
      // 4. launch operation
      [anOp main];
      // 5. ASSERT mock delegate is called with nil data
      STAssertNoThrow([mockDelegate verify], @"Delegate should be called with nil data and nil URL");
      [anOp release];
    }

Is it the expected behaviour ? or am I doing something wrong ? thanks !


Solution

  • OCMock throws exceptions to report mismatches, trusting OCUnit to catch and report any exceptions. But due to a bug in the iOS Simulator, unit tests are unable to catch exceptions, and so simply crash.

    (I'm currently writing a new mocking framework that works around this problem by not relying on exceptions.)