Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plaid - ShowViewModelTest solution
class ShotViewModelTest {
// This CoroutineDispatcher is injected in the ViewModel and use case
private val testCoroutineDispatcher = TestCoroutineDispatcher()
@After
fun tearDown() {
testCoroutineDispatcher.cleanupTestCoroutines()
}
@Test
fun loadShot_emitsTwoUiModels() = testCoroutineDispatcher.runBlockingTest {
// 1) Given coroutines have not started yet and the View Model is created
testCoroutineDispatcher.pauseDispatcher()
val viewModel = ... // Creates viewModel injecting testCoroutineDispatcher
// Then the fast result has been emitted
val fastResult = viewModel.shotUiModel.getOrAwaitValue()
assertTrue(fastResult.formattedDescription.isEmpty())
// 2) When the coroutine starts
testCoroutineDispatcher.resumeDispatcher()
// 3) Then the slow result has been emitted
val slowResult = viewModel.shotUiModel.getOrAwaitValue()
assertTrue(slowResult.formattedDescription.isNotEmpty())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.