How to add a new Unit Test target and OCMock to an existing XCode project

I had some trouble today trying to add Unit Tests to an existing project that uses CocoaPods. So I decided to do a quick writeup of how I got it working:

  1. Create a new project called ‘BetterTodoList’

  2. Create a new Podfile with the following contents

platform :ios, 6.0
pod 'AFNetworking'
  1. Close BetterTodoList in XCode, and open the workspace (as instructed by CocoaPods)

  2. Run, and see that everything still works

  3. Write some legacy code

  4. Realize you need Unit Tests:

    • File -> New -> Target
    • Choose Other -> Cocoa Touch Unit Test Bundle
    • Name it “BetterTodoListTests”
  5. Press Cmd+U. If nothing happens, check if Product > Test is grayed out in the menu. In that case, press Cmd+<, select Test and add the test using the + button.

  6. Add the following lines to your Podfile, and re-run “pod install”

    target 'BetterTodoListTests', :exclusive => true do
    pod 'OCMock' 
  7. Import OCMock.h in one of your files and see that it’s working.

  8. ?

  9. Profit

Addendum: If you still have problems because the test target cannot access the compiled objects (e.g. “Symbol Not Found” errors), select the project in the organizer, then select the Test target, go to “Bundle Loader” and add the following values for both Debug and Release: $(BUILT_PRODUCTS_DIR)/