使用waitForExpectations Crash SwiftUI

Posted

技术标签:

【中文标题】使用waitForExpectations Crash SwiftUI【英文标题】:Use of 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% 明确的:“在等待时调用等待测试用例” 【参考方案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

以上是关于使用waitForExpectations Crash SwiftUI的主要内容,如果未能解决你的问题,请参考以下文章

waitForExpectations 失败时在 UITest 上取得成功

Storybook 和 AntDesign 组件 - 如何使用 CRA 和 Typescript 进行设置?

使用 wmonk vs Microsoft 的 Typescript 创建 React App (CRA)?

使用 CRA 时解决 JSX 语法错误(创建反应应用程序)

什么是 CRA ?

Jest 手动模拟在 CRA 中不使用模拟文件