如果重写 UIView 中的drawRect,IOS 单元测试会抛出异常

标签 ios sentestingkit

我正在编写一个应用程序,其中需要重写 UIViews 中的 drawRect 函数以将不同的形状绘制到应用程序中。

我通常使用内置的 SenTestingKit 对我的所有应用程序进行单元测试。一旦我包含任何 contextRefCG 绘图内容,每当我尝试运行测试时,我都会看到一系列异常,并且测试不会运行。如果我注释掉 drawRect 函数,测试将再次编译并运行。

我看到的错误消息是:

Undefined symbols for architecture i386:
  "_CGContextAddArc", referenced from:
      -[HeartShapeView drawRect:] in HeartShapeView.o
  "_CGContextAddLineToPoint", referenced from:
      -[SquareShapeView drawRect:] in SquareShapeView.o
      -[HeartShapeView drawRect:] in HeartShapeView.o
  "_CGContextBeginPath", referenced from:
      -[SquareShapeView drawRect:] in SquareShapeView.o
      -[HeartShapeView drawRect:] in HeartShapeView.o
  "_CGContextClosePath", referenced from:
      -[SquareShapeView drawRect:] in SquareShapeView.o
      -[HeartShapeView drawRect:] in HeartShapeView.o

这是我的 DrawRect 的 SquareShapeView 之一中的代码

- (void)drawRect:(CGRect)rect
{
    CGFloat small = rect.size.width;
    small = (rect.size.height <= rect.size.width) ? rect.size.height : small;

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 0.0f, 0.0f);
    CGContextAddLineToPoint(context, 0.0f, small);
    CGContextAddLineToPoint(context, small, small);
    CGContextAddLineToPoint(context, small, 0.0f);

    CGContextClosePath(context);
    CGContextSetLineWidth(context, 1.0f);

    CGContextStrokePath(context);
}

最佳答案

您忘记将 CoreGraphics 框架包含到您的测试目标中

关于如果重写 UIView 中的drawRect,IOS 单元测试会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065685/

相关文章:

ios - 在 SKScene 中更改 SKSpriteNode 的纹理

ios - 为什么 SenTestingKit 导致我的应用程序崩溃?

cocoa - 添加 OCMock 后单元测试始终为 "successful"

ios - 无法删除所有带动画的 UITableView 单元格

ios - 测试钥匙串(keychain) - OSStatus 错误 -34018

objective-c - @throw 之后保留计数耗尽

jquery - iOS5 + jquery-mobile 过渡闪烁

ios - 为什么在函数外部引用不创建保留循环?

iphone - 仅在为设备编译时变量 "undeclared"