Testing autolayout with XCTest

自作多情 提交于 2019-12-10 14:18:48

问题


I'm trying to figure out if there is a way to test the layout of an iOS view in unit tests when using autolayout. Right now I try to simply initialize the view controller, and then check the frames of the views. However, the frame on each view remains origin=(x=0, y=0) size=(width=0, height=0).

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

I've tried adding

[_viewController.view needsUpdateConstraints];

or

[_viewController.view updateConstraints];

or

[_viewController updateViewConstraints];

or

[_viewController viewWillAppear:YES];

but none of them have helped.

Is it at all possible to get autolayout to run when using XCTest?


回答1:


Have you tried setNeedsLayout followed by layoutIfNeeded?

You can definitely get layout to run in tests, I do it here, but that doesn't have a view controller, just views.



来源:https://stackoverflow.com/questions/26632534/testing-autolayout-with-xctest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!