I'm running a UI test where I need to test an firebase phone authentication function using the waitForExpectations API. Currently i'm using two waitForExpectations, In the first command working fine but in second command the Code goes to crash.
Code:-
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)
}
}
}
Code ScreenShot:-

Error:-
Thread 1: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API violation - calling wait on test case while already waiting.'"
CodePudding user response:
You're already scheduling a wait on testVerifyOTP(), Remove the one used at the verify function:
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)
}
}
CodePudding user response:
Declare in the class not in the function.
Code:-
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)
}
}
