美好的一天, 我正在使用 OCHamcrest 和 OCMockito 开始我使用 TDD 方法的道路,它们真的很酷,但有一件事我似乎不了解模拟协议(protocol)并尝试评估参数。
考虑代码附带的示例:
- (void)testVerifyWithMatcherForPrimitive
{
NSMutableArray *mockArray = mock([NSMutableArray class]);
[mockArray removeObjectAtIndex:2];
[[verify(mockArray) withMatcher:greaterThan([NSNumber numberWithInt:1]) forArgument:0]
removeObjectAtIndex:0]; // The 0 is a placeholder, replaced by the matcher
}
它按预期工作,但是当我尝试使用模拟协议(protocol)复制它时,它总是通过测试,例如:
- (void)testFirstParameter
{
// given
id<TestProtocol> mockElement = mockProtocol(@protocol(TestProtocol));
// when
id toProcess = nil;
[mockElement process: toProcess];
// then
id firstArgumentMatcher = instanceOf([NSArray class]);
[[verify(mockElement) withMatcher: firstArgumentMatcher forArgument: 0] process: toProcess];
}
在这个测试中,我试图测试参数是 NSArray 类的实例,它不应该通过。你能帮我检测一下我做错了什么吗?
非常感谢,
路易斯·亚历杭德罗
最佳答案
我觉得你想要
[[verify(mockElement) process:instanceOf([NSArray class])];
仅对原始参数使用 withMatcher:forArgument:
。
关于ios - 使用 OCMockito 测试发送到模拟协议(protocol)的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928906/