ios - 如何测试自定义 UINavigationController 行为

标签 ios swift unit-testing uinavigationcontroller tdd

我正在尝试在我的测试中测试一些 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/

相关文章:

Django 测试 : determine which view was executed

objective-c - 如何使 UIImageView 围绕其中心旋转

xcode - 在 Swift 中为 UILongPressGestureRecognizer 添加图像或文本?

swift - 将类型统一在一个协议(protocol)下

python - 在 Python 中编写 Selenium 单元测试的正确方法?

ios - Swift:使用 NSNotificationCenter 进行测试

ios - 适用于 Android 的 Firebase 推送通知,但仅适用于 iOS 的 Firebase 控制台

ios - XMPPFramework - 如果尚未创建 vCard,则不会更新昵称

iOS:多行 uilabel 只显示一行自动布局

ios - Swift - 使用 CoreML 删除图像背景