I am having an issue with my repository tests. This is the first time I've really used Firebase, and they come with coroutines out of the box now, so I thought I'd use them
But when I am setting up a test, it is hanging forever when calling the coroutines await()
Here are the contents of my test class, followed by the method I am testing.
@JvmField
@Rule
val rule = InstantTaskExecutorRule()
@get:Rule
val testCoroutineRule = TestCoroutineRule()
private lateinit var subject: UserDataRepository
@MockK
private lateinit var firestoreRef: FirebaseFirestore
@MockK
private lateinit var firebaseAuth: FirebaseAuth
@MockK
private lateinit var firebaseStorage: FirebaseStorage
@MockK
private lateinit var userDao: UserDao
@MockK
private lateinit var context: Context
@MockK
private lateinit var authResult: AuthResult
private val USERS = "Users"
private val user = UserData("Jo Bloggs")
private val email = "itsyaboijo@gmizzle.com"
private val password = "iLovefluffycats863$"
private val username = user.name
private val uid = "d98dy1x/;10v84i1[,'1"
private val data = hashMapOf("name" to user.name)
@Before
fun setup() {
MockKAnnotations.init(this)
every { context.getExternalFilesDir(null)?.absolutePath } returns "testing/path/child"
subject = UserDataRepository(
context,
firestoreRef,
firebaseAuth,
firebaseStorage,
userDao
)
}
@Test
fun `when createUser called successfully then returns AuthResult`() {
testCoroutineRule.runBlockingTest {
//GIVEN
coEvery { userDao.addUser(user) } returns 1
coEvery { firebaseAuth.createUserWithEmailAndPassword(email, password).await() } returns authResult
coEvery { firestoreRef.collection(USERS).document(uid).set(data).await() }
//WHEN
val actualAuthResult = subject.createUser(email, password, username)
//THEN
Assert.assertEquals(authResult, actualAuthResult)
}
}
Tested Method:
suspend fun createUser(email: String, password: String, username: String): AuthResult {
userDao.addUser(UserData(username))
val data = hashMapOf(
"name" to username
)
val authResult = firebaseAuth.createUserWithEmailAndPassword(email, password).await()
firestoreRef.collection("Users")
.document(firebaseAuth.currentUser!!.uid)
.set(data).await()
return authResult
}
Aucun commentaire:
Enregistrer un commentaire