如何将结构参数与OCMock匹配?

时间:2014-04-08 09:52:46

标签: ios objective-c struct ocmock

所以我在名为PopoverProvider

的类中有一个具有以下签名的方法
- (void)showPopoverForAction:(MESAction *)action fromRect:(CGRect)rect inView:(UIView *)view onDoneBlock:(MESActionPopoverDoneBlock)block;

我想验证它是这样被调用的:

// popoverProvider is a PopoverProvider
                            [[[popoverProvider expect] andReturn:mockRecipeIngredientViewController]
                                    showPopoverForAction:[OCMArg any]
                                                fromRect:[OCMArg any] // !!!!
                                                  inView:[OCMArg any]
                                             onDoneBlock:[OCMArg any]
                            ];

标记的行会导致问题:[OCMArg any]CGRect s上不匹配,这是一种结构类型。

1 个答案:

答案 0 :(得分:4)

在这种情况下,由于你不关心rect的值,我会使用:

[[[[popoverProvider expect] andReturn:mockRecipeIngredientViewController] ignoringNonObjectArgs]
                 showPopoverForAction:[OCMArg any]
                             fromRect:CGRectMake(0.0f,0.0f,0.0f,0.0f)
                               inView:[OCMArg any]
                          onDoneBlock:[OCMArg any]
];

如果你有一个案例,你想忽略一个结构,但检查另一个结构,你可以这样做:

[[[[[popoverProvider expect] andReturn:mockRecipeIngredientViewController] ignoringNonObjectArgs]  andDo:^(NSInvocation *invocation) {
        CGRect theSize;
        [invocation getArgument:&theSize atIndex:6];
        STAssertEquals(theSize, CGSizeMake(20.0f,20.0f), @"No match!");
    }]
                 showPopoverForAction:[OCMArg any]
                             fromRect:CGRectMake(0.0f,0.0f,0.0f,0.0f)
                               inView:[OCMArg any]
                          onDoneBlock:[OCMArg any]
                             someSize:CGSizeMake(0.0f,0.0f)
];