【问题标题】:Setting expectation on NSMutableAttributedString对 NSMutableAttributedString 设置期望
【发布时间】:2012-11-13 16:08:12
【问题描述】:

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

【问题讨论】:

    标签: ios unit-testing nsmutablestring


    【解决方案1】:
    [[mockController expect] temp] 
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多