2014-10-29 2 views
3

autolayout을 사용할 때 유닛 테스트에서 iOS보기의 레이아웃을 테스트 할 수있는 방법이 있는지 알아 내려고하고 있습니다. 지금은 단순히보기 컨트롤러를 초기화 한 다음보기의 프레임을 확인하려고합니다. 그러나 각보기의 프레임은 원점 = (x = 0, y = 0) 크기 = (너비 = 0, 높이 = 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]; 

또는

추가하려고했습니다

그러나 아무도 도움이되지 않았습니다.

XCTest를 사용할 때 autolayout을 실행할 수 있습니까?

+0

'setNeedsLayout' 다음에'layoutIfNeeded'를 시도해 보셨습니까? 테스트에서 실행되도록 레이아웃을 만들 수 있습니다. https://github.com/jrturton/UIView-Autolayout/blob/master/Example/AutoLayoutTests/AutoLayoutConstraintsSpec.m 여기에는 뷰 컨트롤러가 없습니다. 조회수. – jrturton

+0

완벽하게 작동했습니다. 고맙습니다. – Rubberduck

+0

OK, 답변으로 추가하겠습니다. – jrturton

답변

4

setNeedsLayout을 시도한 후에 layoutIfNeeded을 시도 했습니까?

테스트에서 실행되도록 레이아웃을 얻을 수는 있지만 here 일뿐입니다. 뷰 컨트롤러가없고보기 만 있습니다.