【问题标题】:issue in writing Unit test case for UIFont Categories为 UIFont 类别编写单元测试用例的问题
【发布时间】:2012-11-13 19:06:36
【问题描述】:

我已经向UIFont 类添加了方法 .h 文件

  @interface UIFont (UIFont_CustomisedFont)
    +(UIFont*)bold11;
    +(UIFont*)bold12;
    +(UIFont*)bold13;

.m 文件

+(UIFont*)bold11{
  return [UIFont boldSystemFontOfSize:11];
}
+(UIFont*)bold12{
  return [UIFont boldSystemFontOfSize:12];
}
+(UIFont*)bold13{
  return [UIFont boldSystemFontOfSize:13];
}

我在UIFont 类别中添加了很多方法

对于上述方法,我使用OCUnitTest编写了单元测试用例

-(void)testBold11
{
    @try {
        UILabel *lbl = [[UILabel alloc] init];
        lbl.font = [UIFont bold11];
    }    
    @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        STFail(@"testBold11 failed");
    }
}

其他函数也有类似的 UnitTestCases

当我运行UnitTest 时,它并没有崩溃,而是在一个断点处停止,这条消息即将到来Thread 1: EXC_BREAKPOINT(code=EXC_1385_BPT,subcode=0*0

我没有设置任何断点,我在 "release" 模式下运行,而不是在调试模式下。

请帮助我解决这个问题。

【问题讨论】:

  • 我也有同样的情况……有什么解决办法吗?

标签: iphone xcode ios5 xcode4 iphone-developer-program


【解决方案1】:

创建一个更好的测试用例,其中不涉及无关的UILabel类:

-(void)testBold11
{
    @try {
        UIFont *font = [UIFont bold11];
        STAssertNotNil(font, @"Failed to create font");
        STAssertEquals(font.pointSize, 11.0, @"Wrong point size");
    }    
    @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
        STFail(@"testBold11 failed");
    }
}

【讨论】:

    【解决方案2】:

    您需要添加一个测试主机应用程序(查找“测试主机”和“捆绑加载程序”)。如果您没有这样的测试主机,某些 UIKit 方法会在这样的 HALT 条件下崩溃。

    最简单的就是 main.m 和 info.plist。请参阅我的博客文章,其中解释了 how to set up a minimalistic bundle loader 以解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      相关资源
      最近更新 更多