OCMock is a framework for using mock objects in Objective-c projects. If you’re not familiar with mock objects, refer to mockobjects.com. For more detailed instructions, refer to the mock objects section in the Unit Testing iOS Applications in Xcode 4 course on lynda.com. The framework and source code are available on ocmock.org and also on github. Here are the basic steps for adding it to your project:
- Download the OCMock dmg file from the ocmock.org download page.
- Double-click the dmg file to extract the files from it.
- Add a Libraries group to your project at the top level.
- Select the Libraries group, and select Files -> Add Files to <project name> (or Cmd+Opt+A)
- Navigate to the OCMock files just downloaded. These will appear under “Devices” on the left.
- Select the libOCMock.a file and OCMock folder inside the iOS folder.
- Set the “Copy items into destination group’s folders” checkbox.
- Set the “Add to targets” checkboxes for the projects unit test target(s).
- Clear the main project executable target.
- Click Add.
- Open the build settings for the test target, and add “$(SRCROOT)/OCMock” to the Header Search Paths setting.
- Also add “$(SRCROOT)” to the Framework Search Path.
- Add -all_load to the Other Linker Flags build setting for the test target also.
And that’s it (at least as of Xcode 4.4.1).
Add #import OCMock.h to a test file to verify that the header search path is set correctly, and/or that you’ve copied the OCMock folder to your project correctly.
Instantiate a mock object in one of your test to verify that the everything is working.
id mock = [OCMockObject mockForClass:NSObject.class];
If you get a link error when building the tests containing this, check that the libOCMock.lib file was copied to your project directory.