我正在尝试在我的测试中测试一些 UINavigationController 自定义子类委托(delegate)行为,但委托(delegate)方法永远不会在测试内触发。我已经尝试将 appdelegate 窗口 rootViewController 设置为导航并访问 .view 属性,但推送 View Controller 永远不会触发 willShow 委托(delegate)方法。
它在非测试应用程序上调用(运行应用程序并导航)。
这是一个代码示例:
let firstController = UIViewController()
let secondController = UIViewController()
let navigationController = BurgerNavigationController(rootViewController: firstController)
navigationController.pushViewController(secondController, animated: true)
expect(secondController.navigationItem.backBarButtonItem).notTo(beNil())
我希望导航 Controller 调用它
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
委托(delegate)方法。运行应用程序时会调用它,但我不知道如何测试它。
这是我正在使用的 UINavigationController 的自定义初始化:
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
delegate = self
}
并且已实现但从未在同一个类中调用委托(delegate)方法:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//Code here never called on tests but called on real app
}
最佳答案
我发现了这个问题。 UINavigationController 委托(delegate)是异步的,因此您应该等待它被调用。
就我而言,我使用 Nimble 进行测试,因此使用 .toEventually
而不是 .to
有效。现在等待期望成为现实。
关于ios - 如何测试自定义 UINavigationController 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020432/