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");
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With