gpt4 book ai didi

unit-testing - Swift - 涉及 IBOutlets 的单元测试功能?

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:58 27 4
gpt4 key购买 nike

我正在为我的 Swift 项目设置单元测试,但在测试涉及更新 IBOutlets 的类函数时遇到了问题。

我有一个函数 validateUrl,它需要传递一个字符串,然后对其进行验证。如果有效,则启用 UIButton,如果无效,则禁用 UIButton。当我运行调用此函数的测试时,应用程序在启用或禁用 UIButton 的代码行崩溃。 Storyboard和 Controller 都有适当的测试目标集。

这行代码:

    self.submitButton.enabled = true// Enable Submit Button

吐出这个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

尝试使用此代码来初始化 View Controller 的 IbOutlets:

   let yourStoryboard = UIStoryboard(name: "Your_storyboard", bundle: nil)
yourViewController = yourStoryboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
yourViewController.loadView() // This line is the key

关于unit-testing - Swift - 涉及 IBOutlets 的单元测试功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740945/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com