0

我正在为返回 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 设置期望值?

4

1 回答 1

1
[[mockController expect] temp] 

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

[[[mockController expect] andReturn:temp ] theMethodThatYouHaveTheIfStatementIn]
于 2013-06-18T19:40:14.957 回答