Abhimanyu
09/28/2024, 3:42 AMupdateAccount()
method?
fun updateAccount() {
coroutineScope.launch {
startLoading()
val isAccountUpdated = updateAccountUseCase()
if (isAccountUpdated) {
navigator.navigateUp()
} else {
completeLoading()
// TODO: Show Error
}
}
}
// For context
fun startLoading() {
isLoading = true
refreshSignal.tryEmit(Unit)
}
fun completeLoading() {
isLoading = false
refreshSignal.tryEmit(Unit)
}
More details in thread 🧵 .
Can anyone please help thank you color .Abhimanyu
09/28/2024, 3:44 AM@Test
fun `updateAccount when updateAccountUseCase returns true`() = testScope.runTest {
whenever(updateAccountUseCase()).thenReturn(true)
SUT.refreshSignal.test {
SUT.updateAccount()
assertEquals(Unit, awaitItem())
assertEquals(true, SUT.isLoading)
expectNoEvents()
}
verify(navigator).navigateUp()
}
But this fails, ❌
@Test
fun `updateAccount when updateAccountUseCase returns false`() = testScope.runTest {
whenever(updateAccountUseCase()).thenReturn(false)
SUT.refreshSignal.test {
SUT.updateAccount()
assertEquals(Unit, awaitItem())
assertEquals(true, SUT.isLoading) // This assertion fails as the isLoading is reset by this time (my guess)
assertEquals(Unit, awaitItem())
assertEquals(false, SUT.isLoading)
expectNoEvents()
}
}
Abhimanyu
09/28/2024, 3:45 AMprivate val testDispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(testDispatcher)
Abhimanyu
09/28/2024, 3:46 AMAbhimanyu
09/28/2024, 1:29 PMDmitry Khalanskiy [JB]
09/30/2024, 7:58 AMupdateAccountUseCase
immediately returns false
, the two emissions will be conflated into one. Insert a delay into the mocked method to simulate that the call does not finish immediately, this will make the test pass.