测试显示和消失的动画标签的文本



我正在努力测试标签(toastLabel(的外观,当有人输入错误的电子邮件时,它会短暂地出现在视野中。

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?()
}

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

更新1:

因此,在玩了一会儿之后,我可以看到,当我点击 registerButton 时,吐司会正常显示,但测试不会继续,直到它再次消失。我觉得这很奇怪,因为它没有严格附加到注册按钮上,这是它自己的观点。

更新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(,您应该注入其将同步运行的模拟/存根或使用XCTestCase.expectation并祈祷 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)
}

最新更新