Sam de Freyssinet

An archive of thoughts and discoveries in the world of software development

Adding OCMock to Xcode 4 Projects

Today I have been adding some new functionality to an iPhone application I am currently developing for work. Being the good developer that I am (Ed: and modest too), I started by writing some tests to cover the changes I needed to make. These days Xcode ships with the SenTest Objective C unit testing framework, which is great. But SenTest does not include a mocking framework. Fortunately OCMock exists to solve this very problem. However getting it into your iOS project and running takes a little bit of effort.

First of all grab the latest copy of the OCMock dmg from, version 2.0.1 at the time of writing. Next mount the disk image ocmock-2.0.1.dmg (version number may be different) so that the directory structure of the mounted disk image should resemble the image below

At this point the OCMock library is ready to be added to your project. The disk image provided supplies linked libraries for iOS and Mac OS X, as well as the source. Naturally the project source is also available on Github. Be warned, the Github source will require compiling before it can be used in your own tests. If you are not comfortable with that concept, it is best just to use the precompiled binaries.

Before importing the OCMock library into your Xcode 4 project, create a folder for this library name something similar to $(SRCROOT)/tests/libraries. $(SRCROOT) is xcode shorthand for the project root folder for anyone scratching their head. In Xcode create a group within the tests group called libraries to mirror the layout of the filesystem. Now drag the contents of the OCMock iOS folder on the mounted drive to the new libraries group within Xcode created previously.

Xcode might be clever and realize what has just happened at this point and added libOCMock.a to your tests targets Link Binary With Libraries build phase. If it has not, then you can drag libOCMock.a from your filetree into the Link Binary With Libraries build phase to add it. Before continuing we need to ensure that Xcode is including the library and headers when the tests are run. Within the test target, check the Build Settings entry for Header Search Paths and Library Search Paths includes $(SRCROOT)/tests/libraries.

Finally and most importantly Xcode needs to tell the compiler to force load the OCMock libraries at compile time, otherwise your tests will crash. Within the Build Settings in your test build target add the following line to the Other Linker Flags entry; -force_load $(SRCROOT)/tests/libraries/libOCMock.a.

OCMock is now ready for use and bar any other unforseen problems, testing should be uneventful. The last step of instructing the compilier to force load the library caused me to loose 30 minutes this afternoon, so hopefully this will help anyone else having similar issues.