ios - 在 iOS 中更新 UI 元素的单元测试方法
问题描述
我正在尝试为我现有的代码编写单元测试。我有3种方法。
func methodOne() {
// code
methodTwo()
}
func methodTwo() {
// code
methodThree()
}
func methodThree() {
// code
// update UI element
}
对此类方法进行单元测试的理想方法是什么。在单元测试methodTwo()
时,它methodThree()
会在未加载 UI 元素时调用,它们在被调用nil
时具有价值methodThree()
。如何对涉及 UI 元素的方法进行单元测试。我不想测试 UI 元素是否加载正确,我只想测试methodTwo()
and中的代码methodThree()
。有没有办法绕过 UI 元素相关的代码。任何帮助表示赞赏。谢谢你。
解决方案
做到这一点的最好方法是尽可能多地将要测试的代码与 UI 分开。这可以通过将 的//code
部分methodTwo
放入它自己的函数(即methodFour
)并从 调用它来完成methodTwo
。然后您的代码将如下所示:
func methodTwo() {
methodFour()
methodThree()
}
func testMethodFour() {
methodFour()
// check assumptions
}
func methodFour() {
// code that you originally had in methodTwo before methodThree
}
推荐阅读
- reactjs - 用 Enzyme 测试 ReduxForm
- visual-studio - 使用 Visual Studio 2017 的 Wix 3.11 安装错误
- r - 在 Ubuntu 系统上的 R 中安装 Sodium 包时出现问题
- python - Python argparse - 两个可变长度的参数
- javascript - 如何获取有关电子 webview 崩溃事件的信息
- google-cloud-firestore - 什么是 { get },为什么我会收到“预期 '{' 开始 getter 定义”错误?
- python - Spyder 4.1.2 (Python 3.8) 空白屏幕
- sql - sql查询返回空表
- c++ - 请求“m”中的成员“find”,它是非类类型“int”
- java - 使用 SpringBoot 内置的日志服务记录完整的请求正文