Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing autolayout with XCTest

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?

like image 832
Rubberduck Avatar asked Oct 29 '14 14:10

Rubberduck


People also ask

Is XCTest a unit testing framework?

XCTest framework is one of those frameworks that enables its users to write basic unit, performance and some level of UI tests for iOS apps.

What is XCTest used for?

Overview. Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

What is swift XCTest?

XCTest is a framework by Apple that helps us create and run unit, performance, and UI tests. For now, we'll focus on creating unit tests only. The framework provides us with two major classes: XCTest which acts as the base class for creating, managing, and executing tests.


1 Answers

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.

like image 99
jrturton Avatar answered Oct 15 '22 02:10

jrturton