我有一个iOS应用程序,我正在为其创建单元测试。行使的许多方法都会创建一个UibezierPath的实例,并在视图的绘制方法中被调用。测试通过,但我看到了输出中这种错误的许多实例:
<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.
我想这是由于这些方法在绘制方法之外被调用,但这就是单位测试的全部内容!尽管测试通过,而消息现在只是一个烦人,但日志也被它们混乱了。任何有意义的数据都会丢失。我可以创建图形上下文以供单元测试使用吗?还有其他方法可以摆脱消息吗?谢谢!
编辑:这是正在测试的方法
- (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;
}
和单元测试:
- (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];
}
和错误:
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()
?