0
votes

We generate Obj-C code from CoralClient, and here is a method from CoralCall class:

- (id)call:(CoralModel *)request error:(NSError * __autoreleasing *)error;

And here is basically my code to use CoralCall:

Request *request = ...;

// Make the Coral call
NSError *error = nil;
int count = 0;
do {
    // There's no result, because this API won't return anything
    [coralCall call:request error:&error];
    ++count;
} while (error != nil && count < MAX_RETRY_COUNT);

completionHandler(error);

How can I use OCMock to mock the coralCall object to return an error in the [call:error:] method? I want to unit test the retry logic, and there seem to be no way to achieve this.

Note: I can easily mock the success of the call, but not failure, like this:

// Doesn't matter what the returned value is, so use nil for simplicity
OCMStub([self.coralCall call:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]]).andReturn(nil);

UPDATED: this is now resolved with Erik's answer. In case anyone need an answer, here is how to mock the error:

NSError *error = OCMClassMock([NSError class]);
OCMStub([self.coralCall call:[OCMArg any] error:[OCMArg setTo:error]]).andReturn(nil);
1

1 Answers

0
votes

It looks like what you want to do is described in the reference documentation in section 2.5 (Returning values in pass-by-reference arguments): http://ocmock.org/reference/#stubing-methods