is there any way to invoke a block with nil
as a given argument, given that the invokeBlockWithArgs:
requires the args to be nil-terminated?
example method definition in a mocked object:
- (void)methodWithCompletion:(void(^)(NSString*, NSError* )) completionBlock;
The given mockObject should call:
completionBlock(@"foo", nil);
however, with invokeBlockWithArgs
:
OCMStub([mockObj methodWithCompletion:([OCMArg invokeBlockWithArgs:@"foo", nil, nil])]);
Method fails, with too few arguments; obviously with nil being the termination, it doesn't recognize the second parameter to the block should be nil
.
I haven't tested it but theoretically passing [NSNull null]
should work.
Adding to the existing answers here, passing [NSNull null]
does what you want in this case, which is passing nil
as the param there.
I had a case (shown below) where my logic tested the existence of an error object OR my array was empty, and wanted my test to cover both cases and was afraid I'd only be able to test one case
if (error || array.count == 0) {
// fail here
}
Here is my test OCMock code:
NSArray *emptyArray = @[];
OCMStub([requestMock loadListWithCompletion:([OCMArg invokeBlockWithArgs:emptyArray, [NSNull null], nil])]);
...and in the actual invocation of that method, the error param (that I passed [NSNull null]
into) was indeed nil
, so the logic fell through to the empty array and the error case was still handled.
You can pass [NSNull null]. I just tested, it works.
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