I've been using the following post as guidance for how to display a UIAlertController
from code that is unrelated to a specific UIViewController
. Now, I want to unit test this code:
func showAlert(alert: UIAlertController, animated: Bool, completion: (()->Void)?)
{
let alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
// Keep a strong refence to the window.
// We manually set this to nil when the alert is dismissed
self.alertWindow = alertWindow
alertWindow.rootViewController = UIViewController()
if let currentTopWindow = UIApplication.sharedApplication().windows.last {
alertWindow.windowLevel = currentTopWindow.windowLevel + 1
}
else {
// This case only happens during unit testing
Logger.trace(ICELogLevel.Error, category: .Utility, message: "The application doesn't have a window being displayed!")
}
// preload the viewController for unit testing
// (see https://www.natashatherobot.com/ios-testing-view-controllers-swift/ )
let _ = alertWindow.rootViewController?.view
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController!.presentViewController(self.alertController, animated: animated, completion: completion)
}
However, when running a unit test, on the alertWindow.makeKeyAndVisible()
line, I get an NSInternalInconsistencyException: props must have a valid clientID
.
This code works in the app code, and I'd prefer not to use a mock UIWindow etc, since I'm looking to verify that the alert is actually shown on a real UIWindow.
Any guidance on how we can use UIWindows() in Unit Tests? What am I doing wrong?