gpt4 book ai didi

swift - 测试出现和消失的动画标签的文本

转载 作者:搜寻专家 更新时间:2023-10-30 22:13:37 25 4
gpt4 key购买 nike

我正在努力测试标签 (toastLabel) 的外观,当有人输入错误的电子邮件时,该标签会以动画形式短暂显示在 View 中。

private func registerNewUser(email: String, password: String, confirmationPassword: String) {
if password == confirmationPassword {
firebaseData.createUser(email: email, password: password, completion: { (error, _ ) in
if let error = error {
self.showToast(in: self.view, with: error.localizedDescription)
} else {
self.showToast(in: self.view, with: "Registered succesfully")
self.signInUser(email: email, password: password)
}
})
} else {
//raise password mismatch error
print("password mismatch error")
}
}

func showToast(in toastSuperView: UIView, with text: String) {
let toastLabel = ToastLabel()
toastLabel.text = text
toastSuperView.addSubview(toastLabel)
layoutToastLabel(toastLabel)
animateToastLabel(toastLabel)
}

private func layoutToastLabel(_ toastLabel: ToastLabel) {
toastLabel.centerYToSuperview()
toastLabel.pinToSuperview(edges: [.left, .right])
}

private func animateToastLabel(_ toastLabel: ToastLabel) {
UIView.animate(withDuration: 2.5, delay: 0, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: { _ in
toastLabel.removeFromSuperview()
})
}

我只想测试在用户输入一封已被接收的电子邮件后,是否会出现从 firebase 收到的错误文本。

func testRegisteringWithUsedEmailDisplaysFirebaseError() {
let email = registeredEmail
let password = "password"

welcomeScreenHelper.register(email: email,
password: password,
confirmationPassword: password,
completion: {

let firebaseErrorMessage = "The email address is already in use by another account."
XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
})
}

func register(email: String, password: String, confirmationPassword: String, completion: (() -> Void)? = nil) {
let emailTextField = app.textFields[AccesID.emailTextField]
let passwordTextField = app.secureTextFields[AccesID.passwordTextField]
let confirmPasswordTextField = app.secureTextFields[AccesID.confirmPasswordTextField]
let registerButton = app.buttons[AccesID.registerButton]

emailTextField.tap()
emailTextField.typeText(email)
passwordTextField.tap()
passwordTextField.typeText(password)
registerButton.tap()
confirmPasswordTextField.tap()
confirmPasswordTextField.typeText(confirmationPassword)
registerButton.tap()

completion?()
}

当我使用 expectation 和 XCTWaiter 等其他工具时,尽管文本和标签确实出现了,但测试仍然没有通过。我从来没有做过这样的测试,所以我不确定我可能哪里出错了,我是否必须做一些不同的事情来测试动画 View 或其他东西。

更新 1:

所以我可以看到,当我点击 registerButton 时,我可以看到,当我点击 registerButton 时,toast 会正常显示,但测试不会继续,直到它再次消失。我觉得这很奇怪,因为它并没有严格依附于 registerButton 作为它自己的 View 。

更新2:

我已经更新我的测试如下:

func testRegisteringWithUsedEmailDisplaysFirebaseError() {

welcomeScreenHelper.register(email: registeredEmail,
password: password,
confirmationPassword: password,
completion: {

let firebaseErrorMessage = "The email address is already in use by another account."

let text = self.app.staticTexts[firebaseErrorMessage]
let exists = NSPredicate(format: "exists == true")

self.expectation(for: exists, evaluatedWith: text, handler: nil)
self.waitForExpectations(timeout: 10, handler: nil)
XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
})
}

加上:

override func setUp() {
app.launch()
UIView.setAnimationsEnabled(false)
super.setUp()
}

override func tearDown() {
if let email = createdUserEmail {
firebaseHelper.removeUser(with: email)
}
UIView.setAnimationsEnabled(true)
super.tearDown()
}

但到目前为止还没有运气。我仍然可以在 func register 中看到,一旦点击注册按钮,toast 就会显示,并且在 toastLabel 完成动画之前不会调用下一行。

最佳答案

在这种测试中你需要解决几个问题:

  1. 如果您正在测试的代码正在使用 DispatchQueue.async,您应该使用 XCTestCase.expectation
  2. 如果您正在测试的代码中包含 UIView.animate(我看到您的示例中有一个),请在测试之前执行 UIView.setAnimationsEnabled(false) 并且在测试完成后启用它,这样期望就不会等待动画完成。您可以在 XCTestCase.setUpXCTestCase.tearDown 方法中完成。
  3. 如果您正在测试的代码具有依赖项,例如执行异步调用的服务(我假设 firebaseData 是),您应该注入(inject)它们将同步运行的模拟/ stub 或使用 XCTestCase。期望并祈祷API/网络在测试运行时一切正常。

所以使用 XCTestCase.expectation + UIView.setAnimationsEnabled(false) 应该适合你。具有足够高超时的 XCTestCase.expectation 也应该可以工作。

编辑 1:期望值的正确使用方法:

func test() {
let exp = expectation(description: "completion called")
someAsyncMethodWithCompletion() {
exp.fulfill()
}
waitForExpectations(timeout: 1) { _ in }
// assert here
}

所以你的测试方法应该是:

func testRegisteringWithUsedEmailDisplaysFirebaseError() {
let exp = expectation(description: "completion called")
welcomeScreenHelper.register(email: registeredEmail,
password: password,
confirmationPassword: password,
completion: { exp.fulfill() })
self.waitForExpectations(timeout: 10, handler: nil)
let firebaseErrorMessage = "The email address is already in use by another account."
XCTAssert(self.app.staticTexts[firebaseErrorMessage].exists)
}

关于swift - 测试出现和消失的动画标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878079/

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