Issue
For an integration test I want to do some tests with a very newly created user account. For that I create a Firebase user in my test case function like that:
@Test
fun registerAndSigningInWithVeryNewUser() {
val auth = Firebase.auth
auth.createUserWithEmailAndPassword(strEmail, strPassword)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success")
// doing some test stuff
} else {
// If sign in fails
Log.w(TAG, "createUserWithEmail:failure", task.exception)
assert(false)
}
}
}
The new user account is created correctly. I can verify that in the Firebase Authentication dashboard. But the task in addOnCompleteListener is never called. I could not understand why.
Update: If I use the debugger and go step by step through the test function, the listener is called. So I think I need some idle code to wait for the async database call
Any hints?
Solution
The problem was, that the called function is an asynchronous call to a remote server (of cause).
The test case ends before the response could arrive. So I added an ugly Thread.sleep(10000).
@Test
fun registerAndSigningInWithVeryNewUser() {
val auth = Firebase.auth
auth.createUserWithEmailAndPassword(strEmail, strPassword)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success")
// doing some test stuff
} else {
// If sign in fails
Log.w(TAG, "createUserWithEmail:failure", task.exception)
assert(false)
}
}
Thread.sleep(10000)
}
But using Thread.sleep(...) is not always good: https://www.repeato.app/android-espresso-why-to-avoid-thread-sleep/ ore https://developer.android.com/training/testing/espresso/idling-resource
Answered By - Manuel Siebeneicher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.