How to create a stub for isKindOfClass?

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

How to create a stub for isKindOfClass?

Postby oredi » 14 Nov 2010, 15:57

I tried looking through the forums and google but I seem to identify what I wanted. I am trying to Mock the CCLocationManager class from the CoreLocation framework. I was trying to call the isKindOfClass method on the mock object but it threw an exception 'NSInvalidArgumentException', the reason is -[NSInvocation getArgumentAtIndexAsObject:]: unrecognized selector sent to instance. I tried using the various OCArg to stub the method but it failed. May I know if anyone has been able to do this kind of stubs. The signature of the method is - (BOOL)isKindOfClass:(Class)aClass. Thank you for any assistance in advance.

Code: Select all
managerMock = [OCMockObject mockForClass:[CLLocationManager class]];
[[[managerMock stub] andReturn:OCMOCK_VALUE(noValue)] isKindOfClass:[CLLocationManager class]];   

I have attached a snippet of the test code I have written for the mock object.

The following is the error message
Code: Select all
Test Suite 'LocationServiceLogicTests' started at 2010-11-17 23:04:05 +0900


2010-11-17 23:04:05.449 otest[473:903] *** -[NSInvocation getArgumentAtIndexAsObject:]: unrecognized selector sent to instance 0xd91830


2010-11-17 23:04:05.449 otest[473:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSInvocation getArgumentAtIndexAsObject:]: unrecognized selector sent to instance 0xd91830'

2010-11-17 23:04:05.450 otest[473:903] Stack: (
    4626512,
    2855724,
    4635227,
    4097654,
    4094450,
    12848332,
    12852206,
    12852055,
    862637,
    12856127,
    12837607,
    4094557,
    4094266,
    537937556,
    537939305,
    537937045,
    537948041,
    537937045,
    537948041,
    537937045,
    537946195,
    9079,
    9634,
    8266
)

terminate called after throwing an instance of 'NSException'
/Developer/Tools/RunPlatformUnitTests.include: line 415:   473 Abort trap              "${THIN_TEST_RIG}" "${OTHER_TEST_FLAGS}" "${TEST_BUNDLE_PATH}"
Command /bin/sh failed with exit code 1
oredi
 
Posts: 2
Joined: 14 Nov 2010, 15:51

Re: How to create a stub for isKindOfClass?

Postby oredi » 18 Nov 2010, 16:36

It was a setup problem for me.

The following links resolve the issue for me:
1. Setup Unit Testing - http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/UnitTesting/1-Articles/CreatingTests.html#//apple_ref/doc/uid/TP40002171-BBCBGHCJ
2. Fixed bug in xCode - http://stackoverflow.com/questions/3516745/sentestcase-in-xcode-3-2-and-xcbuildlogcommandinvocationsection-errors
3. Setup OCMock - http://kgriff.posterous.com/pages/setting-up-ocmock
oredi
 
Posts: 2
Joined: 14 Nov 2010, 15:51

Re: How to create a stub for isKindOfClass?

Postby erik » 28 Jan 2011, 20:42

This looks like you've missed some linker flags and the categories from OCMock are not loaded into your test project.

This seems to be a common issue. I've now added a small check to the initialiser of OCMockObject that raises an exception with a message describing the problem if the category methods aren't present.

erik
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany


Return to OCMock



cron