I'm trying to create tests with mockk for my suspend functions that include await(). I have tried the following code so far, but the test never ends.
private lateinit var repository: AuthRepository
private lateinit var databaseReference: DatabaseReference
private lateinit var auth: FirebaseAuth
@Before
fun setUp() {
mockkStatic(FirebaseAuth::class)
auth = mockk()
databaseReference = mockk()
every { FirebaseAuth.getInstance() } returns auth
repository = AuthRepository(databaseReference)
}
@Test
fun login_Successful() = runBlocking {
val email = "test@example.com"
val password = "password"
val authResult: AuthResult = mockk()
val expectedResult = Resource.Success(authResult)
coEvery { auth.signInWithEmailAndPassword(email, password).await() } returns authResult
val result = repository.login(email, password)
Assert.assertEquals(expectedResult, result)
}
Is the approach correct or am I missing something?
This is the code to test:
private val auth = FirebaseAuth.getInstance()
suspend fun login(email: String, password: String): Resource<AuthResult> =
try {
val authResult = auth.signInWithEmailAndPassword(email, password).await()
Resource.Success(authResult)
} catch(e: FirebaseAuthException) {
Resource.Error(e.errorCode)
}