A common construct in Objective-C code is alloc init:
[[NSSomeObject alloc] init]
This line allocates memory and initializes an Apple framework object (as indicated by the NS prefix). So how do we test this? Should we test this?
I will assert that we should not test this. It is Apple’s code, let them test it. Our unit tests should be concerned with testing the code that we write. So what do we do with our code that includes such constructs?
We have at least a couple options. We could essentially ignore that its there, and unit test the code containing it. This would be suboptimal from a performance perspective, and would cause problems if the init code has other dependencies, for example the presence of other files, data models, etc.
I suggest that a better approach is to extract the code to a separate method. Once isolated there, the method can be intercepted using a partial mock, and a mock object returned instead. I’ll provide an example of doing this in an upcoming Core Data example.