Can OCMock run a block parameter?

前端 未结 5 1830
予麋鹿
予麋鹿 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:18

    EDIT 2: Use https://stackoverflow.com/a/32945785/637641 instead.

    Using andDo: is perfectly fine, but personally I prefer [OCMArg checkWithBlock:].

    [[mock expect] theMethod:[OCMArg checkWithBlock:^BOOL(id param)
    {
        void (^passedBlock)( BOOL ) = param;
        // Normally I set some expectations here and then call the block.
        return YES;
    }]];
    
    // Code to test
    
    [mock verify];
    

    You can use also [mock stub] but I prefer to verify that theMethod is called.

    EDIT 1

    OCMock 3 version:

    OCMExpect([mock theMethod:[OCMArg checkWithBlock:^BOOL(void (^passedBlock)(BOOL))
    {
        // call the block...
        return YES;
    }]]);
    
    // Code to test
    
    OCMVerify(mock);
    

提交回复
热议问题