iossentestingkit

IOS Unit tests throw exception if overriding drawRect in a UIView


I am writing an app that involves me overriding the drawRect function in UIViews to draw different shapes into the App.

I normally unit test all of my apps using the inbuilt SenTestingKit. As soon as I include any contextRefCG drawing stuff, whenever I try and run the tests I see a series of exceptions and the tests don't run. If I comment out the drawRect function the tests will compile and run again.

The error messages I see are:

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

Here is the code that is in one of my my SquareShapeView for my drawRect

- (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);
}

Solution

  • You forget to include CoreGraphics framework into your test target