Search code examples
unit-testingocmock

How does OCMock works?


We've been using OCMock for unittesting and it works very nicely. We're just not sure how does it work. Monkey patching? Code-generation on preprocess?

I've tried Googling it without success.

Anyone knows how does OCMock do it magic?


Solution

  • For instance methods on normal mocks, OCMock uses forwardInvocation: and friends. This resembles dynamic proxies or method missing from other languages. For further details you can start at the code here:

    For class methods and partial mocks, OCMock uses monkey patching. At runtime it creates dynamic subclasses of either the class (for partial mocks) or the meta class (for class methods), and then switches the dynamic class into use. Entry points into the code:

    For setting up mocks and stubs OCMock heavily relies on C macros. It uses a neat trick with blocks to implement a syntax with dots and round brackets. I first saw this in Expecta.