Adding OCMock to a Project

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:

  1. Download the OCMock dmg file from the ocmock.org download page.
  2. Double-click the dmg file to extract the files from it.
  3. Add a Libraries group to your project at the top level.
  4. Select the Libraries group, and select Files -> Add Files to <project name> (or Cmd+Opt+A)
  5. Navigate to the OCMock files just downloaded. These will appear under “Devices” on the left.
  6. Select the libOCMock.a file and OCMock folder inside the iOS folder.
  7. Set the “Copy items into destination group’s folders” checkbox.
  8. Set the “Add to targets” checkboxes for the projects unit test target(s).
  9. Clear the main project executable target.
  10. Click Add.
  11. Open the build settings for the test target, and add “$(SRCROOT)/OCMock” to the Header Search Paths setting.
  12. Also add “$(SRCROOT)” to the Framework Search Path.
  13. 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.

Leave a Reply