Testing autostart with XCTest

I am trying to figure out if there is a way to check the layout of an iOS view in unit tests when using autoplay. Right now I'm trying to just initialize the view controller and then check the view frames. However, the border 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 tried adding

[_viewController.view needsUpdateConstraints];

      

or

[_viewController.view updateConstraints];

      

or

[_viewController updateViewConstraints];

      

or

[_viewController viewWillAppear:YES];

      

but none of them helped.

Is it even possible to trigger autostart when using XCTest?

+3


source to share


1 answer


Have you tried setNeedsLayout

and then layoutIfNeeded

?



You can definitely get a mock to run in tests, I'm doing it here , but this doesn't have a view controller, just views.

+6


source







All Articles