使用XCTest测试自动布局 [英] Testing autolayout with XCTest

查看:51
本文介绍了使用XCTest测试自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定是否有一种方法可以在使用自动布局时在单元测试中测试iOS视图的布局.现在,我尝试简单地初始化视图控制器,然后检查视图的框架.但是,每个视图上的帧仍为原点=(x = 0,y = 0)大小=(宽度= 0,高度= 0).

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

我尝试添加

[_viewController.view needsUpdateConstraints];

[_viewController.view updateConstraints];

[_viewController updateViewConstraints];

[_viewController viewWillAppear:YES];

但他们都没有帮助.

使用XCTest时是否可以自动布局运行?

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

推荐答案

您尝试过setNeedsLayout,然后是layoutIfNeeded吗?

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.

这篇关于使用XCTest测试自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆