3
我想知道是否有方法在使用自动布局时在单元测试中测试iOS视图的布局。现在我试着简单地初始化视图控制器,然后检查视图的框架。但是,每个视图上的帧仍然是原点=(x = 0,y = 0)size =(width = 0,height = 0)。使用XCTest测试自动布局
- (void)setUp {
[super setUp];
_viewController = [[AddPlayerViewController alloc] init];
_viewController.view.frame = CGRectMake(0, 0, 320, 460);
[_viewController view];
}
- (void)testViewsAreInsideWindow {
[self checkIfViewIsInsideWindow:_viewController.txtfNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.btnNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.tblPlayers];
}
- (void)checkIfViewIsInsideWindow:(UIView *)view {
CGRect windowFrame = _viewController.view.frame;
CGRect viewFrame = view.frame;
XCTAssertTrue(CGRectContainsRect(windowFrame, viewFrame));
}
我已经尝试添加
[_viewController.view needsUpdateConstraints];
或
[_viewController.view updateConstraints];
或
[_viewController updateViewConstraints];
或
[_viewController viewWillAppear:YES];
但他们都没有帮助。
使用XCTest时,是否有可能使自动布局运行?
您是否尝试过'setNeedsLayout'后跟'layoutIfNeeded'?你可以让布局在测试中运行,我在这里执行:https://github.com/jrturton/UIView-Autolayout/blob/master/Example/AutoLayoutTests/AutoLayoutConstraintsSpec.m但没有视图控制器,只是观点。 – jrturton 2014-10-29 15:44:29
完美的工作。谢谢。 – Rubberduck 2014-10-30 09:38:06
好吧,我会将其添加为答案 – jrturton 2014-10-30 12:09:50