Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTest how to perform segue that present a modal view and test presentedViewController

I was trying to test a segue that would present a view controller. But, it always fail. How to wait for the segue to complete? Please assist.

[self.viewController performSegueWithIdentifier:@"mySegueName" sender:self.viewController];
XCTAssertNotNil(self.viewController.presentedViewController, @"Failed to show modal view");
like image 235
Saran Avatar asked Jan 31 '26 07:01

Saran


1 Answers

The problem is that you're trying to present on a UIViewController whose view is not in the UIWindow hierarchy. Here's my solution:

- (void)testExample {

    //
    // Arrange
    // Storyboard
    //
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    //
    // Arrange
    // View Controller
    //
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [UIApplication sharedApplication].keyWindow.rootViewController = viewController;

    //
    // Act
    //
    [viewController performSegueWithIdentifier:@"ModalSegue" sender:nil];

    //
    // Assert
    //
    XCTAssertNotNil(viewController.presentedViewController);

}
like image 75
Rudolf Adamkovič Avatar answered Feb 01 '26 22:02

Rudolf Adamkovič



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!