Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i mock a method that accepts a handle as an argument in OCMock?

I'm trying to mock a method that has the equivalent of the following signature:

- (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError **)error

I want it to return a small dictionary so that my test can make sure the code uses the dictionary properly. however, no matter what i do OCMock always returns nil from the method, regardless of how i stub it out. The error begins as nil in the code i'm testing, and these are the different ways i've tried stubbing it:

NSError * error = nil;
[[[mock stub] andReturn:someDict] uploadValues:YES error:&error];

[[[mock stub] andReturn:someDict] uploadValues:YES error:nil];

[[[mock stub] andReturn:someDict] uploadValues:YES error:[OCMArg any]];

and none of them work. Does OCMock support handles as stubbed message arguments and if so, what's the correct way to do it?

like image 984
Kevlar Avatar asked Aug 17 '09 16:08

Kevlar


2 Answers

[[[mock stub] andReturn:someDict] uploadValues:YES error:[OCMArg setTo:nil]];

or

NSError* someError = ...
[[[mock stub] andReturn:someDict] uploadValues:YES error:[OCMArg setTo:someError]];

You could also do

[[[mock stub] andReturn:someDict] uploadValues:YES error:[OCMArg anyPointer]];

but this might cause your code to incorrectly think that you passed back a real NSError.

like image 166
Andreas Järliden Avatar answered Sep 21 '22 01:09

Andreas Järliden


With ARC, your method declaration will probably look like this:

- (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError *__autoreleasing *)error;

Here is how I mock these types of methods:

BOOL mockDoSomething = YES;

NSError __autoreleasing *error = nil;

[[[mock stub] andReturn:someDict] uploadValues:OCMOCK_VALUE(mockDoSomething) error:&error];
like image 38
Eric Avatar answered Sep 23 '22 01:09

Eric