Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created October 9, 2019 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelvicnt/6807564f0b3e684512aa844d2fc0562c to your computer and use it in GitHub Desktop.
Save manuelvicnt/6807564f0b3e684512aa844d2fc0562c to your computer and use it in GitHub Desktop.
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