我正在测试一个显示UIAlertController的函数,但是测试一直失败。这是函数的样子:
func presentBuyingErrorDialogue() {
let alert = UIAlertController(
title: "Warning",
message: "Error purchasing item, please retry this action. If that doesn't help, try restarting or reinstalling the app.",
preferredStyle: .alert
)
let okButton = UIAlertAction(title: "OK", style: .default)
alert.addAction(okButton)
self.present(alert, animated: true, completion: nil)
}
因为这个函数在一个叫做ShopViewController的类里,我认为正确的测试方式应该是调用函数shopViewController.presentBuyingErrorDiaologue()
,然后使用XCTAssertTrue(shopViewController.presentedViewController is UIAlertController)
。然而,当我运行测试时,断言语句失败了。那么正确的测试UIAlertController是否是当前显示的视图的方法是什么?
print(UIApplication.shared.keyWindow?.rootViewController)
,它显示了什么? - mugx