I have a method that I need to stub. The method is of the form below:
BOOL myMethodWithError:(*__autoreleasing *NSError)error;
So I mocked the object and attempted to return a nil back through 'error'. I coded it as follows.
id mockMyObject = [OCMockObject mockForClass:[MyObject class]];
BOOL retVal = YES;
NSError *error = nil;
[[[mockMyObject stub] andReturn:OCMOCK_VALUE(retVal)] myMethodWithError:&error];
When the test is run and the mock object is operated, the error reference id appears to change. So the mock throws an exception:
OCMockObject[MyObject]: expected method invoked: myMethodWithError:0xbfffca78
I have tried a number of different ways but each time the pointer value appears to change once the error object is passed to the method which causes the mock object to throw an error.
I simply need to test my business rules against the pass-by-reference value of the argument, but I can't seem to get the mock or test object to cooperate.
Thanks in advance, any help will be greatly appreciated.
Apart from the solutions Ben mentions in his answer(I only tested the one based on ignoringNonObjectArgs, and it works fine), I prefer to use [OCMArg anyPointer] with the appropriate cast:
[[[myMock stub] andReturn:something] someMethod:(NSError * __autoreleasing *)[OCMArg anyPointer]];
Use ignoringNonObjectArgs
[[[[mock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] ignoringNonObjectArgs] myMethodWithError:NULL];
From http://ocmock.org/features/
Arguments that are neither objects nor pointers or selectors cannot be ignored using an any placeholder. It is possible, though, to tell the mock to ignore all non-object arguments in an invocation:
[[[mock expect] ignoringNonObjectArgs] someMethodWithIntArgument:0]
In this case the mock will accept any invocation of someMethodWithIntArgument: no matter what argument is actually passed. If the method has object arguments as well as non-object arguments, the object arguments can still be constrained as usual using the methods on OCMArg.
Bonus Answer
This would also solve your issue:
[[[mock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] myMethodWithError:[OCMArg setTo:nil]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With