对于原始论点,你必须做一点舞蹈.假设我们模拟了NSMutableArray,并希望验证调用
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
Run Code Online (Sandbox Code Playgroud)
代替
[verify(mockArray) replaceObjectAtIndex:[argument capture] withObject:anything()];
Run Code Online (Sandbox Code Playgroud)
这给你类型冲突,我们只有一个虚拟值(0会很好)但添加一个OCMockito调用来覆盖给定参数索引处的匹配器:
[[verify(mockArray) withMatcher:[argument capture] forArgument:0]
replaceObjectAtIndex:0 withObject:anything()];
Run Code Online (Sandbox Code Playgroud)
-withMatcher:forArgument:对于第一个参数,参数索引是基于0的,因此这表示"对于第一个参数,忽略传入的任何内容并使用此匹配器".
还有一种方法-withMatcher:可以在第一个参数上执行此操作,因此可以将此示例简化为
[[verify(mockArray) withMatcher:[argument capture]]
replaceObjectAtIndex:0 withObject:anything()];
Run Code Online (Sandbox Code Playgroud)