我正在努力测试标签(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等其他工具时,尽管文字和标签肯定出现,但测试仍然没有通过。我从来没有做过这样的测试,因此我不确定我可能会出错的地方,我是否必须做一些不同的事情来测试动画视图或其他东西。
UPDATE1:
所以我可以看到经过更多的游戏后,当我点击registerButton时,吐司就会出现,但测试不会继续,直到它再次消失。我发现这很奇怪,因为它没有严格依附于registerButton是它自己的观点。
UPDATE2:
我更新了我的测试内容如下:
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完成动画制作之前不会调用下一行。
答案 0 :(得分:4)
在这种测试中你需要解决几件事:
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)
}