Search code examples
iosunit-testingocmock

OCMock expect a method called within another method


This is an extremely basic question about OCMock expectations. Let's you have an instance method methodA on objectA that calls an instance method methodB on objectA.

- (void)methodA {
    [self methodB];
}

- (void)methodB {
    ...
}

Now, let's say I want to verify that a partial mock of objectA invokes methodA and methodB. It seems like all you should have to do is:

- (void)test {
   id mockObjectA = [OCMockObject partialMockForObject:self.objectA];
   [[mockObjectA expect] methodA];
   [[mockObjectA expect] methodB];

   [self.objectA methodA];

   [mockObjectA verify];
}

When I run the test, it thinks that methodA was successfully invoked but that methodB was not successfully invoked. What am I misunderstanding?

Thanks for the help.


Solution

  • You need to tell the mock to forward the method call on to the real object so the implementation will actually run.

    [[[mockObjectA expect] andForwardToRealObject] methodA];