如何在SwiftUI中获取UI测试中的EnvironmentObject/Publishers



如果变量从false切换到true,我正在尝试为SwiftUI签名页编写UI测试,并尝试从类中检查发布者。我所做的是将应用程序作为模块@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完全兼容,并且您可以保留所有现有代码。

相关内容

  • 没有找到相关文章

最新更新