【问题标题】:Use of waitForExpectations Crash SwiftUI使用waitForExpectations Crash SwiftUI
【发布时间】:2022-01-09 14:57:33
【问题描述】:

我正在运行 UI 测试,我需要使用 waitForExpectations API 测试 Firebase 电话身份验证功能。目前我正在使用两个waitForExpectations,在第一个命令中工作正常,但在第二个命令中代码崩溃。

代码:-

func testsendOTPAndVerify() {
    let expection = expectation(description: "OTP Send With Correct Number")
    let signupClassMthd = SignupScreen(phoneNumber: .constant("9814012345"))
    signupClassMthd.verify { response, verificationId in
        XCTAssert(response == true)
        if response {
            expection.fulfill()
            self.testVerifyOTP(verificationID: verificationId)
        }
    }
    self.waitForExpectations(timeout: 30) { respoError  in
        if let errors =  respoError {
            print("OTP Send ",errors.localizedDescription)
        }
    }
}


func testVerifyOTP(verificationID:String){
    let expection = expectation(description: "Check OTP")
    let verfyClassTest = VerficationCode(phoneNumber: .constant(CommonAllString.BlankStr), verificationID: .constant(verificationID))
    verfyClassTest.verifyPhoneNumberAndLogin(OtpEndrdCode: "000000") { response, responseBool in
        if response == true && responseBool == false {
            expection.fulfill()
        }
        XCTAssert(response == true && responseBool == false)
    }
    self.waitForExpectations(timeout: 30) { respoError  in
        if let errors =  respoError {
            print("Check OTP = ",errors.localizedDescription)
        }
    }
}

代码截图:-

错误:-

线程 1:“由于未捕获的异常 'NSInternalInconsistencyException' 导致应用程序终止,原因:'API 违规 - 在测试用例上调用等待而已经在等待。'”

【问题讨论】:

  • 但这是 100% 明确的:“在等待时调用等待测试用例”

标签: swift firebase unit-testing firebase-authentication


【解决方案1】:

在类中声明而不是在函数中。

代码:-

let signInVerifyUsrExpc = XCTestExpectation(description: "signInVerifyUsrExpc")

func testsendOTPAndVerify() {
    signInVerifyUsrExpc.expectedFulfillmentCount = 2
    let signupClassMthd = SignupScreen(phoneNumber: .constant("9814012345"))
    signupClassMthd.verify { response, verificationId in
        XCTAssert(response == true)
        if response {
            self.testVerifyOTP(verificationID: verificationId)
            self.signInVerifyUsrExpc.fulfill()
        }
    }
    wait(for: [signInVerifyUsrExpc], timeout: 100.0)
}


func testVerifyOTP(verificationID:String){
    let verfyClassTest = VerficationCode(phoneNumber: .constant(CommonAllString.BlankStr), verificationID: .constant(verificationID))
    verfyClassTest.verifyPhoneNumberAndLogin(OtpEndrdCode: "000000") { response, responseBool in
        if response == true && responseBool == false {
            self.signInVerifyUsrExpc.fulfill()
        }
        XCTAssert(response == true && responseBool == false)
    }
}

【讨论】:

    【解决方案2】:

    您已经在 testVerifyOTP() 上安排等待,请删除在验证功能中使用的那个:

    func testsendOTPAndVerify() {
        let expection = expectation(description: "OTP Send With Correct Number")
        let signupClassMthd = SignupScreen(phoneNumber: .constant("9814012345"))
        signupClassMthd.verify { response, verificationId in
            XCTAssert(response == true)
            if response {
                expection.fulfill()
                self.testVerifyOTP(verificationID: verificationId)
            }
        }
        self.waitForExpectations(timeout: 30) { respoError  in
            if let errors =  respoError {
                print("OTP Send ",errors.localizedDescription)
            }
        }
    }
    
    
    func testVerifyOTP(verificationID:String){
        let expection = expectation(description: "Check OTP")
        let verfyClassTest = VerficationCode(phoneNumber: .constant(CommonAllString.BlankStr), verificationID: .constant(verificationID))
        verfyClassTest.verifyPhoneNumberAndLogin(OtpEndrdCode: "000000") { response, responseBool in
            if response == true && responseBool == false {
                expection.fulfill()
            }
            XCTAssert(response == true && responseBool == false)
        }
    }
    

    【讨论】:

    • 使用上面的代码,我开始担心了。 “由于未等待的期望‘检查 OTP’而失败。”请检查所附图片。 i.stack.imgur.com/SaUE8.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2023-03-09
    • 2017-04-20
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多