iOS Project Setup
This section was originally written for Xcode 5.1.1 / iOS 7.1.
It applies to newer versions of Xcode, too. The latest confirmed version is Xcode 7.0.1 / iOS 9.0.
OCMock static library
OCMock comes in two flavours, a framework and a static library. Please use the static library for iOS development. The framework is for developing OS X applications and tools.
When a test project links against the static library, the OCMock classes are included in the binary of the test project, which can be run in the simulator or an iOS device. The library included in the binary releases of OCMock is built fat, containing binary code for Intel CPUs (for the simulator) and ARM CPUs (for running on the device).
Adding the OCMock static library to an iOS project
Project layout
To use the library in an iOS project, you have to make the actual library, ie. libOCMock.a
, as well as the header files available to the target containing the tests. In the iOS7 example project we're using this directory structure:
Adding the library itself
The library must be added to the the test target in the “Link Binaries With Libraries” build phase. Make sure you have selected the test target. Then click on the plus (+) icon, choose “Add other...”, and then find the library in the filesystem. Afterwards, the OCMock library should be visible in the list as shown below.
Setting the linker flags
For static libraries the linker tries to be clever and only includes those symbols that it thinks are used. It gets this wrong with Objective-C categories, so we need to tell it that we're dealing with Objective-C by adding the -ObjC
linker flag as shown below.
If you forget this step you will get an appropriate warning (in the form of an exception) the first time you try to use a mock object in your tests.
Making the headers available
Adding the library as described above automatically adds a library search path to the project so that the linker can find the library. Unfortunately, it does not add a header search path, which is needed for the compiler to resolve includes/imports. This must be done manually in the build settings.
Search for “header search” which should bring up the “Header Search Paths” setting. The best way to point to the header directory is by adding a reference relative to the project's source directory. For the layout described above this would be $(PROJECT_DIR)/usr/include
.
If you have copied the library or headers to a different place these settings must be adjusted accordingly.
The iOS example projects
OCMock contains several example projects, including iOS7 and iOS9 example projects that shows how to use OCMock in iOS unit tests. The project is set up following Apple's guidelines and the linker flags are set as described above. Note that in the Xcode table view the path variables are expanded and the full paths are shown. This can be confusing.
The project contains one unit test that shows how to use mocks to replace a UITableView when testing a controller. It should build and the test should pass. If it doesn't something is very wrong.