在NSMutableAttributedString上设置期望值

时间:2012-11-13 16:08:13

标签: ios unit-testing nsmutablestring

我正在为返回NSMutableAttributedString的方法中的“if”语句编写单元测试,但是我收到了一些错误。

IF语句的实际代码:

if (conditionIsTrue)
    {
        return [[NSMutableAttributedString alloc] initWithString:NSLocalizedStringWithDefaultValue(@"ABC", 
                                                                                                   @"ABCLibrary", 
                                                                                                   [NSBundle bundleWithName:@"ABCLibraryResources"],
                                                                                                   @"No ABC", 
                                                                                                   @"No ABC String") 
                                                      attributes:[self methodA]];
    }

单元测试:

ControllerA *controller = [[ControllerA alloc] init];
id mockController = [OCMockObject partialMockForObject:controller];

NSMutableAttributedString *temp = [[NSMutableAttributedString alloc] initWithString:NSLocalizedStringWithDefaultValue(@"ABC", 
                                                                                                   @"ABCLibrary", 
                                                                                                   [NSBundle bundleWithName:@"ABCLibraryResources"],
                                                                                                   @"No ABC", 
                                                                                                   @"No ABC String") 
                                                      attributes:[self methodA]];

    [[mockController expect] temp];

错误:

No known instance method for selector temp.

我是否错误地设定了预期?如何设置NSMutableAttributedString的期望值?

1 个答案:

答案 0 :(得分:1)

[[mockController expect] temp] 

告诉mockController期望应该调用方法temp。 temp,这里只是一个变量。你真正想要的是

[[[mockController expect] andReturn:temp ] theMethodThatYouHaveTheIfStatementIn]