How to unit test custom drawRect implementation

I'm trying to write test / s using XCTest to preempt a custom implementation of the drawRect method and don't know how.

The code I want to write because of the / s test looks like this:

- (void)drawRect:(CGRect)rect
{
    CGPoint startOfHorizontalLine = (CGPointMake(1.0f, 0.0f));
    CGPoint endOfHorizontalLine = (CGPointMake(1.0f, 10.0f));

    UIColor * lineColour = [UIColor colorWithRed:40.0f/255.0f green:34.0f/255.0f  blue:34.0f/255.0f alpha:1.0f];
    [lineColour setStroke];

    UIBezierPath *horizontalLine = [UIBezierPath bezierPath];
    [horizontalLine moveToPoint:startOfHorizontalLine];
    [horizontalLine addLineToPoint:endOfHorizontalLine];
    [horizontalLine stroke];
}

      

If I need to use the mocking library I have done some research at OCMock.

Thanks for the help.

+3


source to share


1 answer


To test this, you're probably better off pulling the body out to methods that create paths or set up the current drawing context. Then testing becomes pretty simple:

  • For those who build paths, you can give them your own path and then check that it is built as expected.
  • For the drawing context, you will insert a drawing context (possibly a bitmap context) and ensure that the expected context changes (such as stroke width, join style, stroke or fill color, etc.) are by the method under test.


If this is not practical, then you are probably looking at something more like testing a goldsmith where you get a drawing that looks right, bless it like a "gold master", keeping the drawing output as an image, then set up the test suite. to re-draw what should be the same image and compare them. If that fails, then the test will fail.

+1


source







All Articles