erikdoe / ocmock

Mock objects for Objective-C
http://ocmock.org
Apache License 2.0
2.16k stars 606 forks source link

Using OCMStub with [OCMArg invokeBlockWithArgs], got compiler error: Too many arguments provided to function-like macro invocation #308

Closed marcuswu0814 closed 8 years ago

marcuswu0814 commented 8 years ago

I have a class like this:

typedef void *_Nullable(^ CompletionHandleBlock)(MyTaskClass  _Nullable task, id  _Nullable responseObject, NSError * _Nullable error);

@interface MyClass : MySuperClass

- (void)methodWithFirstArg:(NSDictionary*)arg1 arg2:(NSString*)arg2 completionBlock:(CompletionHandleBlock)completionBlock;

@end

And I try to stub this method like this:


MyClass *someObj = [MyClass new];
id partialMock = OCMPartialMock(someObj);

MyTaskClass *fakeTask = [MyTaskClass new];
NSError *fakeError = [NSError new];

OCMStub([partialMock methodWithFirstArg:[OCMArg any]
                                   arg2:[OCMArg any]
                        completionBlock: [OCMArg invokeBlockWithArgs:fakeTask, @"test", fakeError, nil]]);

It's not working and got complier error _Too many arguments provided to function-like macro invocation_

If I try to change the syntax like this:

id x = [OCMArg invokeBlockWithArgs:fakeTask, @"test", fakeError, nil];

OCMStub([partialMock methodWithFirstArg:[OCMArg any] arg2:[OCMArg any] completionBlock:x]);

And the stub working well, it's there had any hint to solve this problem?

Thanks!

erikdoe commented 8 years ago

The documentation has the hint. In section 2.6 it is stated that "Using invokeBlockWithArgs: it is possible to specify which arguments to invoke the block with; non-object arguments must be wrapped in value objects and the expression must be wrapped in round brackets." The code example in that section shows the round brackets, too.