Can OCMock run a block parameter?

前端 未结 5 1868
予麋鹿
予麋鹿 2020-12-29 22:12

Assume a method signature such as the following:

- (void)theMethod:(void(^)(BOOL completed))completionBlock;

I would like to mock this meth

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 22:33

    Using andDo: blocks is sometimes required but for most cases you can use [OCMArg invokeBlock] or [OCMArg invokeBlockWithArgs:].

    In your example you can do the following
    If you don't care about the arguments:

    // Call block with default arguments.
    OCMStub([mock theMethod:[OCMArg invokeBlock]];
    

    If you want to send specific arguments:

    // Call block with YES.
    OCMStub([mock theMethod:([OCMArg invokeBlockWithArgs:@YES, nil])];
    

    Note the nil termination since you can pass multiple arguments to this method. In addition the entire expression must be wrapped in parentheses.

    You can read more about it in the OCMock documentation.

提交回复
热议问题