Using OCMock 1.77 for Unit and Application Test with iOS4 and Xcode 4/SDK4.3 Using OCMock 1.77 for Unit and Application Test with iOS4 and Xcode 4/SDK4.3 xcode xcode

Using OCMock 1.77 for Unit and Application Test with iOS4 and Xcode 4/SDK4.3


  • Download OCMock *.** from http://ocmock.org/#download (At the moment of this writing 1.77)
  • unpack in a OCMock/ (or another folder) --> You will have two folder "Release" & "Source"
  • Copy the "Release/Library" folder inside your xCode project folder
  • Link your file with xcode dragging all the "Library" folder inside your porject
    • Select as the target the application TEST target!
  • Go to the "build settings" of your test target
    • Under "Search Paths" add: +"Header Search Paths" --> insert the path to your OCMock headers(.h) (something like $(PROJECT_DIR)/Library/Headers) +"Library Search Paths" --> insert the path to your OCMock library(.a) (something like $(PROJECT_DIR)/Library/)Note: as path you can also use $(PROJECT_DIR) or $(SRCROOT)/ and select the recursive checkbox to avoid insert the ocmplete path
      • Under "Linking" add: + "Other Link Flags" --> -all_load

Everything should now work correctly. To verifiy:

#import <OCMock/OCMock.h>// simple test to ensure building, linking, // and running test case works in the project- (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.");}- (void)testOCMockFail {id mock = [OCMockObject mockForClass:NSString.class];[[[mock stub] andReturn:@"mocktest"] lowercaseString];NSString *returnValue = [mock lowercaseString];STAssertEqualObjects(@"thisIsTheWrongValueToCheck", returnValue, @"Should have returned the expected string."); }

Many thanks to:

Enrico Bottani


I can reproduce this by removing the -force_load flag. Check to make sure it's set on your test target, not your main target, and that it points to the correct location of the static library. In my case, that's

-force_load $(PROJECT_DIR)/Libraries/libOCMock.a

The other SO question you linked is just saying that when a mock's verify method fails, it throws an exception from a category method on NSInvocation, which causes otest to crash. It still works; the message is just more cryptic than an assertion failure, which XCode labels as an error in your test class.