Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ocmock's invokeBlockWithArgs using a nil argument

Tags:

ocmock

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.

like image 651
cyrix Avatar asked Aug 02 '16 21:08

cyrix


3 Answers

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

like image 109
Erik Doernenburg Avatar answered Sep 27 '22 23:09

Erik Doernenburg


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.

like image 34
Mike Avatar answered Sep 27 '22 22:09

Mike


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

like image 31
Dan Flict Avatar answered Sep 27 '22 22:09

Dan Flict