OCMock可以运行块参数吗? [英] Can OCMock run a block parameter?
问题描述
假设方法签名如下:
- (void)theMethod:(void(^)(BOOL completed))completionBlock;
我想模拟这个方法签名以确保调用该方法,并只调用完成块。我从其他帖子中看到,例如这一个我可以模拟方法调用并接受任何块,但不能运行块。我也知道有一种我可以使用的andDo方法,但我无法弄清楚如何传递一个块并运行它。
I would like to mock this method signature to ensure the method is called, and just call the completion block. I see from other posts like this one that I can mock the method call and accept any block, but not run the block. I also know there is a andDo method that I might be able to use, but I can't figure out how to pass a block in and run it.
任何想法?
谢谢。
推荐答案
你可以使用 [[mock stub]和do:]
来传递调用mocked方法时调用的另一个块:
You can use [[mock stub] andDo:]
like this to pass another block that gets called when your mocked method is called:
void (^proxyBlock)(NSInvocation *) = ^(NSInvocation *invocation) {
void (^passedBlock)( BOOL );
[invocation getArgument: &passedBlock atIndex: 2];
};
[[[mock stub] andDo: proxyBlock] theMethod:[OCMArg any]];
该块获取 NSInvocation
实例您可以查询所有使用的参数。请注意,第一个参数位于索引2,因为在索引0和1处有self和_cmd。
The block gets a NSInvocation
instance from which you can query all the used arguments. Note that the first argument is at index 2 since you have self and _cmd at the indices 0 and 1.
这篇关于OCMock可以运行块参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!