使用 waitForExpectations 导致 SwiftUI 崩溃

Use of waitForExpectations Crash SwiftUI

我正在 运行 进行 UI 测试,我需要使用 waitForExpectations API 测试 firebase phone 身份验证功能。目前我正在使用两个 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)
        }
    }
}

代码截图:-

错误:-

Thread 1: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API violation - calling wait on test case while already waiting.'"

Declare in the class not in the function.

代码:-

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)
    }
}