12

将 OCMock 对象传递给函数调用 isKindOfClass 的方法。但是对于单元测试,返回的值不是模拟类而是 OCMockObject

4

2 回答 2

1

这是一整篇文章,解释了如何准确编写该OCMock isKindOfClass方法(默认情况下不存在)以及如何使用它: http: //blog.carbonfive.com/2009/02/17/custom-constraints-for-ocmock/

于 2013-05-07T09:07:59.753 回答
0

如果要将 OCMock 对象传递给函数调用 isKindOfClass 的方法,则需要创建部分模拟。以下代码可能会对您有所帮助。它对我有用。

-(void)testMyTest
{
    FirstViewController* masterVC = [[FirstViewController alloc]init];
    SecondViewController *second = [[SecondViewController alloc] init];
    id master = [OCMockObject partialMockForObject:second];
    [[master expect] getName:@"PARAM"];
    [masterVC doSomething:master];
    [master verify];

    [masterVC release];
    [second release];
}

FirstViewController 中的 doSomething 方法

-(void)doSomething:(SecondViewController *)detail
{
    if ([detail isKindOfClass:[SecondViewController class]])
    {
        NSString * returnVal = [detail getName:@"PARAM"];
        NSLog(@"returnVal %@",returnVal);
    }
}

另一种选择是模拟 isKindOfClass 方法,因此测试用例将变为

- (void)testMyTest
{
     CalculatorViewController* masterVC = [[CalculatorViewController alloc]init];
     id master = [OCMockObject niceMockForClass:[SecondViewController class]];
     BOOL ret = YES;
     [[[master expect] andReturnValue:OCMOCK_VALUE(ret)] isKindOfClass:[SecondViewController class]];
     [[master expect] getName:@"PARAM"];
     [masterVC doSomething:master];
     [master verify];
}
于 2013-05-07T08:44:53.057 回答