Issue#11 Adjust ContentRepositoryTest by using runTest correctly instead of deprecated TestDispatcher

This commit is contained in:
Gergely Hegedus 2022-01-23 20:05:59 +02:00
parent 9700a09c95
commit 968ccb647d
2 changed files with 17 additions and 25 deletions

View file

@ -78,7 +78,7 @@ internal class FavouriteContentLocalStorageImplTest {
sut.markAsFavourite(ContentId("a")) sut.markAsFavourite(ContentId("a"))
Assert.assertEquals(expected, actual.await()) Assert.assertEquals(expected, actual.getCompleted())
} }
/** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */ /** GIVEN non empty database WHILE observing content WHEN favourite removed THEN change is emitted */
@ -96,6 +96,6 @@ internal class FavouriteContentLocalStorageImplTest {
sut.deleteAsFavourite(ContentId("a")) sut.deleteAsFavourite(ContentId("a"))
Assert.assertEquals(expected, actual.await()) Assert.assertEquals(expected, actual.getCompleted())
} }
} }

View file

@ -6,6 +6,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.fnives.test.showcase.core.shared.UnexpectedException import org.fnives.test.showcase.core.shared.UnexpectedException
import org.fnives.test.showcase.model.content.Content import org.fnives.test.showcase.model.content.Content
@ -126,34 +127,25 @@ internal class ContentRepositoryTest {
} }
sut.fetch() sut.fetch()
Assertions.assertEquals(expected, actual.await()) Assertions.assertEquals(expected, actual.getCompleted())
} }
@DisplayName("GIVEN content response THEN error WHEN fetched THEN only 4 items are emitted") @DisplayName("GIVEN content response THEN error WHEN fetched THEN only 4 items are emitted")
@Test @Test
fun noAdditionalItemsEmitted() { fun noAdditionalItemsEmitted() = runTest {
Assertions.assertThrows(IllegalStateException::class.java) { val exception = RuntimeException()
runTest(UnconfinedTestDispatcher()) { var first = true
val exception = RuntimeException() whenever(mockContentRemoteSource.get()).doAnswer {
val expected = listOf( if (first) emptyList<Content>().also { first = false } else throw exception
Resource.Loading(),
Resource.Success(emptyList()),
Resource.Loading(),
Resource.Error<List<Content>>(UnexpectedException(exception))
)
var first = true
whenever(mockContentRemoteSource.get()).doAnswer {
if (first) emptyList<Content>().also { first = false } else throw exception
}
val actual = async {
sut.contents.take(5).toList()
}
sut.fetch()
Assertions.assertEquals(expected, actual.await())
}
} }
val actual = async(coroutineContext) { sut.contents.take(5).toList() }
advanceUntilIdle()
sut.fetch()
advanceUntilIdle()
Assertions.assertFalse(actual.isCompleted)
actual.cancel()
} }
@DisplayName("GIVEN content response THEN error WHEN fetched THEN only 4 items are emitted") @DisplayName("GIVEN content response THEN error WHEN fetched THEN only 4 items are emitted")