swift - 如何在 SwiftUI 的 UI 测试中获取 EnvironmentObject/Publishers
问题描述
我正在尝试为 SwiftUI 标志页面编写 UI 测试,如果变量从 切换到 ,则尝试从类中检查发布false
者true
。我所做的是将应用程序作为模块导入,@testable import MyApp
然后将此代码编写如下;
func testLogInOfAUser() throws {
let email = app.textFields["email"]
email.tap()
email.typeText("email@mail.com \n")
let password = app.secureTextFields["password"]
password.tap()
password.typeText("password \n")
let loginBtn = app.buttons["Login"]
loginBtn.tap()
let sut = UserAuth()
sut.login()
XCTAssert(sut.isLoggedin == true)
}
类UserAuth
class UserAuth: ObservableObject {
@Published var isLoggedin:Bool = false
func login() {
self.isLoggedin.toggle()
}
}
当我运行测试时,我遇到了这个错误;
Undefined symbols for architecture arm64:
"type metadata accessor for MyApp.UserAuth", referenced from:
MyAppUITests.MyAppUITests.testLogInOfAUser() throws -> () in MyAppUITests.o
"MyApp.UserAuth.__allocating_init() -> MyApp.UserAuth", referenced from:
MyAppUITests.MyAppUITests.testLogInOfAUser() throws -> () in MyAppUITests.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解决方案
XCTest 是一个黑盒测试框架。因此,您只能按照用户与应用交互的方式与应用交互。
在您的情况下,您应该检查 UI 更改,例如登录后是否出现新屏幕等。
如果您仍需要深入研究您的应用程序,则应使用白盒测试框架。我建议您使用 EarlGrey 2.0,因为它与 XCTest 完全兼容,并且您可以保留所有现有代码。
推荐阅读
- python - 使用 librsvg 和 Python 渲染 SVG 时的差异
- archunit - ArchUnit 中的 AnyOf-Predicate
- excel - Excel VBA:从输入的行和列数据中搜索表格并显示在列表框中
- powershell - 如何将开关参数从一个powershell脚本传递到另一个?
- c# - 如何在 ASP.net 核心中手动取消 BackgroundService
- python - 如何使用 pandas 计算每个日期的“中性”、“积极”和“消极”?
- javascript - 将谷歌电子表格结构转换为字典
- python - 如何将字典保存到csv
- list - 如何在迭代期间从列表中减去字符串值?扑
- typescript - 如何检查图片网址是否合规?