如何使用 OCMockito 捕获具有原始值的参数?
MKTArgumentCaptor 似乎只能捕获对象类型?Xcode 说“指向整数转换的指针不兼容”。
对于原始的论点,你必须跳一点舞。假设我们模拟了 NSMutableArray 并想要验证对
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
代替
[verify(mockArray) replaceObjectAtIndex:[argument capture] withObject:anything()];
这会给你类型冲突,我们只有一个虚拟值(0 就可以了),但添加一个 OCMockito 调用以覆盖给定参数索引处的匹配器:
[[verify(mockArray) withMatcher:[argument capture] forArgument:0]
replaceObjectAtIndex:0 withObject:anything()];
第一个参数的参数索引-withMatcher:forArgument:
是从 0 开始的,所以这表示,“对于第一个参数,忽略传入的任何内容并使用此匹配器。”
还有一种方法-withMatcher:
只对第一个参数执行此操作,因此此示例可以简化为
[[verify(mockArray) withMatcher:[argument capture]]
replaceObjectAtIndex:0 withObject:anything()];