OCMOCK_VALUE NSInvalidArgumentException

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.

OCMOCK_VALUE NSInvalidArgumentException

Postby vdaubry » 13 Sep 2010, 17:09

Hi,

I have an issue with stubbing this method :

Code: Select all
- (NSUInteger)loginAttemptForLogin:(NSString*)login;



I tried the following test :

Code: Select all
id mockPreferences = [OCMockObject mockForClass:PrefsManager.class];
[[[mockPreferences stub] andReturnValue:OCMOCK_VALUE(3)] loginAttemptForLogin:[OCMArg any]];
controller.prefmanager = mockPreferences;


It crashes when this method gets called on my mockObject :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Return value does not match method signature.'

Does anybody knows what i'm doing wrong please ?

Thanks,
Vincent.
vdaubry
 
Posts: 1
Joined: 13 Sep 2010, 17:03

Re: OCMOCK_VALUE NSInvalidArgumentException

Postby erik » 13 Sep 2010, 21:21

This is curious. It should not be possible to compile the code you have posted because OCMOCK_VALUE requires a variable and not a constant/literal.

Written as follows the test works for me:
Code: Select all
   NSUInteger three = 3;
   id mockPreferences = [OCMockObject mockForClass:PrefsManager.class];
   [[[mockPreferences stub] andReturnValue:OCMOCK_VALUE(three)] loginAttemptForLogin:[OCMArg any]];

The only way I can provoke the error you describe is by using a different (incorrect) variable type for the value variable, for example:
Code: Select all
   int three = 3;
   id mockPreferences = [OCMockObject mockForClass:PrefsManager.class];
   [[[mockPreferences stub] andReturnValue:OCMOCK_VALUE(three)] loginAttemptForLogin:[OCMArg any]];

This makes sense because you're telling the mock to return the int 3 when the method expects an NSUInteger, which is an unsigned integer. The beauty of C.

By the way PrefManager.class looks really odd to me as a long-time Objective-C programmer; I'd always write it as [PrefsManager class].
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany


Return to OCMock



cron