Using OCMock 1.77 to Test Devices and Applications with iOS4 and Xcode 4 / SDK4.3

I am trying to use OCMock 1.77 to test modules and applications using iOS4 and Xcode 4 / SDK4.3. I followed the instructions for using OCMock as the static library found here: http://www.mulle-kybernetik.com/software/OCMock/ . Testing of the device and applications is performed without OCMock.

When I add OCMock and try to run the OCMock test suite for the simulator (unit tests), my test setup fails with code 134. The test setup works fine for the device (application tests). If I look in the console, I see the message below - it means that I did not add the linker flag -force_load in accordance with the instructions at the above URL. But I have ... Any thoughts?

I looked at this: The test setup came out abnormally with code 134 with OCMock validation on iOS 4 , which suggests that this behavior is a bug - but I'm not sure this is the same situation as I run the OCMock test suite. If this is a mistake, is it possible to use mocks in unit testing?

TIA.

=====

Console output:

3/30/11 1:02:32 AM otest[38552] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '** Expected method not present; the method getArgumentAtIndexAsObject: is not implemented by NSInvocation. If you see this exception it is likely that you are using the static library version of OCMock and your project is not configured correctly to load categories from static libraries. Did you forget to add the -force_load linker flag?' *** Call stack at first throw: ( 0 CoreFoundation 0x004a05a9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x002cf313 objc_exception_throw + 44 2 CoreFoundation 0x00458ef8 +[NSException raise:format:arguments:] + 136 3 CoreFoundation 0x00458e6a +[NSException raise:format:] + 58 4 LogicTests 0x00f89de4 +[OCMockObject initialize] + 115 5 libobjc.A.dylib 0x002cfd9b _class_initialize + 380 6 libobjc.A.dylib 0x002d773f prepareForMethodLookup + 73 7 libobjc.A.dylib 0x002ce069 lookUpMethod + 86 8 libobjc.A.dylib 0x002ce1d6 _class_lookupMethodAndLoadCache + 40 9 libobjc.A.dylib 0x002e10e3 objc_msgSend + 87 10 SenTestingKit 0x20108824 +[NSObject(SenTestRuntimeUtilities) senAllSubclasses] + 107 11 SenTestingKit 0x201074a7 +[SenTestSuite updateCache] + 39 12 SenTestingKit 0x20107443 +[SenTestSuite suiteForBundleCache] + 92 13 SenTestingKit 0x201073a4 +[SenTestSuite testSuiteForBundlePath:] + 108 14 SenTestingKit 0x2010606b +[SenTestProbe specifiedTestSuite] + 332 15 SenTestingKit 0x20106792 +[SenTestProbe runTests:] + 156 16 otest 0x000023c7 0x0 + 9159 17 otest 0x000025f2 0x0 + 9714 18 otest 0x0000209a 0x0 + 8346 19 otest 0x00002049 0x0 + 8265 ) 
+7
source share
2 answers

I can reproduce this by removing the -force_load flag. Make sure it is installed on the target test target, not your main target, and indicates the correct location of the static library. In my case, that

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

the other SO question you linked just says that when the mock verify method fails, it throws an exception from the category method in NSInvocation, which causes otest to fail. It still works; the message is more cryptic than the rejection of an assertion that Xcode has flagged as an error in your test class.

+3
source
  • Download OCMock *. ** from http://ocmock.org/#download (At the time of writing this article 1.77)
  • unzip to OCMock / (or another folder) → You will have two folders "Release" and "Source"
  • Copy the "Release / Library" folder to the xCode project folder.
  • Link your file to xcode by dragging the entire Library folder inside your file
    • Choose the target program TEST!
  • Go to the build settings of your test target
    • In the "Search paths" section, add: + "Header search paths" → insert the OCMock (.h) header path (something like $ (PROJECT_DIR) / Library / Headers) + "Library search paths" → insert the path to your library OCMock (.a) (something like $ (PROJECT_DIR) / Library /) Note: as a path, you can also use $ (PROJECT_DIR) or $ (SRCROOT) / and select a recursive flag to avoid inserting the ocmplete path
      • In the "Binding" section, add: + "Other link flags" → -all_load

Now everything should work correctly. For check:

 #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:

Enrico Bottani

+7
source

All Articles