【发布时间】:2017-02-03 09:03:37
【问题描述】:
我正在尝试在我的测试中测试一些 UINavigationController 自定义子类委托行为,但委托方法永远不会在测试中触发。我已经尝试将 appdelegate 窗口 rootViewController 设置为导航并访问 .view 属性,但推送视图控制器永远不会触发 willShow 委托方法。
在非测试应用上调用(运行应用和导航)。
这是一个代码示例:
let firstController = UIViewController()
let secondController = UIViewController()
let navigationController = BurgerNavigationController(rootViewController: firstController)
navigationController.pushViewController(secondController, animated: true)
expect(secondController.navigationItem.backBarButtonItem).notTo(beNil())
我希望导航控制器将其称为
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
委托方法。它在运行应用程序时被调用,但我不知道如何测试它。
这是我正在使用的 UINavigationController 的自定义初始化:
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
delegate = self
}
并且委托方法已实现但从未在同一个类中调用:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
//Code here never called on tests but called on real app
}
【问题讨论】:
-
请提供一些代码。没有它就很难说什么。
-
您能否分享一下您的自定义导航控制器的相关部分?
-
我已经添加了更多代码,希望你能帮到你。这是一个非常简单的控制器,可以在实际应用中工作,但不能用于单元测试。
标签: ios swift unit-testing uinavigationcontroller tdd