This was harder than I would have liked. Here’s some notes on how to set it up.
Setup OCUnit on your existing project
These notes from Apple are pretty good about how to setup OCUnit on your XCode project.
The only difference was when I ran my unit tests (Product -> Test) I had to manually add my own unit test scheme.

Just hit the ‘Edit Scheme’ button, then hit the ‘+’ sign and click on your target unit tests and click the ‘Add button’.
You should now be able to run your tests against the simulator (not real phone) and see the failure.
Setup OCMock
Create a Libraries directory
Create a ‘Libraries’ directory in your project by right clicking on your project icon and selecting ‘Add Files to …’.

Click the ‘New folder’ button, type the name ‘Libraries’, and click ‘Add’ so your libraries directory is at the same level as your project and unit test target. When your done it should look something like this:

Note: Make sure you select your ‘Test’ target (grey) not the main ‘Project’ target blue. Else you will make all these lovely changes to the wrong config and nothing will work.
Copy in libOCMock.a
Download the libOCMock.a and stick it in the Libraries directory we just created.
Then add it to your project by dragging it into the Libraries directory we just added to your project.
When you do a window will pop up, make sure you select the ‘Copy items’ box at the top and click on your unit test target so it will be linked to OCMock.


Copy OCMock header files
Download the latest OCMock dmg file, located the OCMock directory inside containing the header files, and just like we did before, drag it into the Libraries directory.
Again select ‘Copy’ and click your unit test target and now your directory structure should look like this:

Configure Build Settings
We now need to tell our project how to find these header files we just added.
Click your blue project icon (click somewhere else and then click it again if it doesn’t immediately change) and click the ‘Build Settings’ tab for your project.

In the ‘Search Paths -> Library Search Paths’ section add $(SRCROOT)/Libraries in double quotes to Debug and Release sections. Once you do they will automatically convert to your local machine paths as shown below.
Note: As of XCode 4.3 the Library Search Paths may already be configured for you. The headers ones through aren’t.


And then do the exact same thing for the ‘Search Paths ->Header Search Path’ section (be sure to double click on it after and make sure both are set to ‘recursive’).


Note: As of XCode 4.3 you won’t see the word recursive here – just the box. Click it for both. If you don’t when you run your test you won’t be able to find the OCMock header files.
Next head down to the ‘Linking -> Other Link Flags’ section and add the ‘-all_load’ option.

If you don’t do this you’ll get a “Did you forget to add the -force_load linker flag?” error when you run your tests and you can read about why here.
Add a mock test case
We should now be good to go. Open you the default test case OCUnit created for you and add the following:
#import "UnitTests.h"
#import <OCMock/OCMock.h>
@implementation UnitTests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)testExample
{
//STFail(@"Unit tests are not implemented yet in UnitTests");
}
- (void)testOCMockPass {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:@"mocktest"] lowercaseString];
NSString *returnValue = [mock lowercaseString];
STAssertEqualObjects(@"mocktest", returnValue, @"Should have returned the expected string.");
}
@end
If everything worked, you should be able to run your test now and see a nice failure in your log output window:

Links that help:
https://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1
http://www.raywenderlich.com/3716/unit-testing-in-xcode-4-quick-start-guide
https://developer.apple.com/library/mac/#qa/qa1490/_index.html
http://ocmock.org/
Like this:
Like Loading...