简体   繁体   中英

Invalid Graphics Context during Cocoa-Touch Unit Test

I have an iOS application for which I am creating unit tests. Many of the methods being exercised create an instance of UIBezierPath and are to be called from within the drawRect method of a view. The tests pass, but I see many instances of this type of error in the output:

<Error>: CGContextDrawPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

I would imagine this is due to these methods being called outside the drawRect method, but that's what unit tests are all about! While the tests pass and the messages are just an annoyance now, the log is cluttered with them; any meaningful data is lost. Can I create a Graphics Context for use by the unit tests? Is there any other way to get rid of the messages? Thanks!

EDIT: Here is the method being tested

- (id)visitBBPathRect:(BBPathDataRect *)pathData
{
    const int kNumCorners = 4;
    CGPoint upperLeft = pathData.rect.origin;
    CGPoint upperRight = CGPointMake(
                                     CGRectGetMaxX(pathData.rect),
                                     CGRectGetMinY(pathData.rect));
    CGPoint lowerLeft = CGPointMake(
                                    CGRectGetMinX(pathData.rect),
                                    CGRectGetMaxY(pathData.rect));
    CGPoint lowerRight = CGPointMake(
                                    CGRectGetMaxX(pathData.rect),
                                    CGRectGetMaxY(pathData.rect));
    CGPoint points[kNumCorners] = {upperLeft, upperRight, lowerLeft, lowerRight};
    BBDrawablePathRect *drawableRect = [[BBDrawablePathRect alloc] initWithRect:pathData.rect];
    BBDrawablePath *tempPath = drawableRect;
    for (int i = 0; i < kNumCorners; ++i) {
        tempPath = [self decorateDrawablePath:tempPath withKnot:points[i]];
    }
    return tempPath;
}

And the unit test:

- (void)testVisitBBPathRect
{
    // Given: a drawing visitor and a Rect Path Data to draw
    BBDrawablePathDrawingVisitor *visitor = [BBDrawablePathDrawingVisitor new];
    id pathDataRectMock = [OCMockObject mockForClass:[BBDrawablePathRect class]];
    [[[pathDataRectMock expect] andReturnValue:OCMOCK_VALUE(CGRectMake(0, 0, 0, 0))] rect];
    [[[pathDataRectMock expect] andReturnValue:@YES] isSelected];
    [[pathDataRectMock expect] setBezierPath:[OCMArg any]];

    // When: the visitor draws the path
    id createdPath = [visitor visitBBDrawablePathRect:pathDataRectMock];

    // Then: 
    XCTAssertNotNil(createdPath, @"Visitor should have generated a drawable path");
    XCTAssertTrue([createdPath isKindOfClass:[UIBezierPath class]], @"Drawable path should be a Bezier Path");
    [pathDataRectMock verify];
}

And the error:

Test Case '-[BBPathDataDrawingRepresentationVisitorTests testVisitBBPathRect]' started.
Jan 23 11:08:41 Mercury.local BezierBuilder[1188] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

您是否首先使用上下文调用UIGraphicsPushContext()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM