Skip to content

Instantly share code, notes, and snippets.

@Foxpace
Last active July 3, 2023 20:45
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 Foxpace/6a9096037999eb9ace9973f9511d75c0 to your computer and use it in GitHub Desktop.
Save Foxpace/6a9096037999eb9ace9973f9511d75c0 to your computer and use it in GitHub Desktop.
Example test with turbine
@Test
fun `Given the ViewModel waits - When the event OnLaunch comes, then execute heavy computation with result`() =
// 1.
runTest {
// ARRANGE
val expectedString = "Result"
// 2.
heavyComputation.stub {
onBlocking { doComputation() } doAnswer {expectedString}
}
val sut = ExampleViewModel(heavyComputation)
// 3.
sut.vmState.test {
// ACTION
sut.onEvent(VmEvents.OnLaunch)
// CHECK
// 4.
assertEquals(VmState.Waiting, awaitItem())
assertEquals(VmState.Running, awaitItem())
assertEquals(VmState.Finished(expectedString), awaitItem())
assertEquals(VmState.Waiting, awaitItem())
// the test will finish on its own, because of lambda usage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment