ios - 使用 OCMockito 测试发送到模拟协议(protocol)的参数

标签 ios unit-testing tdd ocmockito ochamcrest

美好的一天, 我正在使用 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/

相关文章:

unit-testing - 可重用的模拟与每个测试中的模拟

ruby - "messy-polymorphism"反模式

ios - UINavigationBar 背景颜色不是我设置的确切 UIColor

ios - 如何让 UIView 在滚动时改变它的大小

ios - 调用 Storyboard数据 - Objective C

c# - 你如何在静态方法中模拟对象

ios - 从 UITableViewCell : addChildViewController unrecognized selector sent to instance 呈现 UIViewController

unit-testing - 测试用例的正确词汇

tdd - 用户故事结束时的集成卡 (TDD)

c# - log4net 和 nunit 测试,最基本的例子