gpt4 book ai didi

ios - 更新 iOS 中 UI 元素的单元测试方法

转载 作者:行者123 更新时间:2023-11-29 05:50:25 25 4
gpt4 key购买 nike

我正在尝试为现有代码编写单元测试。我有3种方法。

func methodOne() {
// code
methodTwo()
}

func methodTwo() {
// code
methodThree()
}

func methodThree() {
// code
// update UI element
}

对此类方法进行单元测试的理想方法是什么?在单元测试 methodTwo() 时,它会调用 methodThree(),因为 UI 元素未加载,当 methodThree() 时它们具有 nil 被调用。如何对涉及 UI 元素的方法进行单元测试。我不想测试 UI 元素是否正确加载,我只想测试 methodTwo()methodThree() 中的代码。有没有办法绕过 UI 元素相关代码。任何帮助表示赞赏。谢谢。

最佳答案

执行此操作的最佳方法是尽可能将要测试的代码与 UI 分开。这可以通过将 methodTwo//code 部分放入其自己的函数(即 methodFour)中并从 methodTwo 调用它来完成。那么你的代码将如下所示:

func methodTwo() {
methodFour()
methodThree()
}

func testMethodFour() {
methodFour()
// check assumptions
}

func methodFour() {
// code that you originally had in methodTwo before methodThree
}

关于ios - 更新 iOS 中 UI 元素的单元测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727108/

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