测试出现和消失的动画标签的文本
Testing text of an animated label that appears and disappears
我正在努力测试标签 (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?()
}
当我使用 expectation 和 XCTWaiter 等其他工具时,尽管文本和标签确实出现了,但测试仍然没有通过。我从来没有做过这样的测试,所以我不确定我可能哪里出错了,我是否必须做一些不同的事情来测试动画视图或其他东西。
更新 1:
所以我可以看到,当我点击 registerButton 时,再玩一会儿后,toast 会正常显示,但测试不会继续,直到它再次消失。我觉得这很奇怪,因为它并没有严格依附于 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 完成动画之前不会调用下一行。
在这种测试中,您需要解决几个问题:
- 如果您正在测试的代码使用
DispatchQueue.async
,您应该使用 XCTestCase.expectation
- 如果您正在测试的代码中包含
UIView.animate
(我看到您的示例中有一个),请在测试前执行 UIView.setAnimationsEnabled(false)
并在测试完成后重新启用它,这样期望就不会了'不要等待动画完成。您可以在 XCTestCase.setUp
和 XCTestCase.tearDown
方法中完成。
- 如果您正在测试的代码具有依赖性,例如正在执行异步调用的服务(我假设
firebaseData
是),您应该注入它们的 mocks/stubs 将同步运行或使用 XCTestCase.expectation
并祈祷 API/network 在测试 运行. 期间一切顺利
所以使用 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)
}
我正在努力测试标签 (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?()
}
当我使用 expectation 和 XCTWaiter 等其他工具时,尽管文本和标签确实出现了,但测试仍然没有通过。我从来没有做过这样的测试,所以我不确定我可能哪里出错了,我是否必须做一些不同的事情来测试动画视图或其他东西。
更新 1:
所以我可以看到,当我点击 registerButton 时,再玩一会儿后,toast 会正常显示,但测试不会继续,直到它再次消失。我觉得这很奇怪,因为它并没有严格依附于 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 完成动画之前不会调用下一行。
在这种测试中,您需要解决几个问题:
- 如果您正在测试的代码使用
DispatchQueue.async
,您应该使用XCTestCase.expectation
- 如果您正在测试的代码中包含
UIView.animate
(我看到您的示例中有一个),请在测试前执行UIView.setAnimationsEnabled(false)
并在测试完成后重新启用它,这样期望就不会了'不要等待动画完成。您可以在XCTestCase.setUp
和XCTestCase.tearDown
方法中完成。 - 如果您正在测试的代码具有依赖性,例如正在执行异步调用的服务(我假设
firebaseData
是),您应该注入它们的 mocks/stubs 将同步运行或使用XCTestCase.expectation
并祈祷 API/network 在测试 运行. 期间一切顺利
所以使用 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)
}