我正在努力测试标签(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时,吐司会按原样出现,但直到再次消失后测试才继续。我发现这很奇怪,因为它并没有严格地附加到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
是),则您应该注入它们将同步运行的模拟/存根,或者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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句