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:
-
Create a new project called ‘BetterTodoList’
-
Create a new Podfile with the following contents
platform :ios, 6.0
pod 'AFNetworking'
-
Close BetterTodoList in XCode, and open the workspace (as instructed by CocoaPods)
-
Run, and see that everything still works
-
Write some legacy code
-
Realize you need Unit Tests:
-
File -> New -> Target
-
Choose Other -> Cocoa Touch Unit Test Bundle
-
Name it “BetterTodoListTests”
-
-
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.
-
-
Add the following lines to your Podfile, and re-run “pod install”
target 'BetterTodoListTests', :exclusive => true do pod 'OCMock' end
-
Import
OCMock.h
in one of your files and see that it’s working. -
?
-
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)/BetterTodoList.app/BetterTodoList