加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:42:07 所属栏目:百科 来源:网络整理
导读:我正在努力测试一个标签(toastLabel)的外观,当有人输入错误的电子邮件时,我会对其进行短暂的动画制作. private func registerNewUser(email: String,password: String,confirmationPassword: String) { if password == confirmationPassword { firebaseData.
我正在努力测试一个标签(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,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,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,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寄存器中,一旦注册按钮被点击,吐司显示并且在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,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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读