【问题标题】:How do I test custom UINavigationController behaviour如何测试自定义 UINavigationController 行为
【发布时间】: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


【解决方案1】:

我找到了问题所在。 UINavigationController 委托是异步的,所以你应该等待它被调用。

在我的情况下,我使用 Nimble 进行测试,因此使用 .toEventually 而不是 .to 有效。它现在等待期望成真。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多