ocmock's invokeBlockWithArgs using a nil argument
Asked Answered
S

3

6

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.

Shabuoth answered 2/8, 2016 at 21:59 Comment(0)
R
9

I haven't tested it but theoretically passing [NSNull null] should work.

Rocket answered 6/8, 2016 at 21:9 Comment(2)
I believe I tested that, and does not work.. Moreso, the code checks "if (error)", which if set to [NSNull null] will always fire; which is not the path to be tested..Shabuoth
The code in OCMock will not pass on the NSNull instance. It will replace it with nil (see: github.com/erikdoe/ocmock/blob/…) Can you please check whether this really doesn't work?Rocket
H
3

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.

Hieronymus answered 1/2, 2017 at 23:24 Comment(0)
T
1

You can pass [NSNull null]. I just tested, it works.

Told answered 24/11, 2016 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.