Skip to content

Instantly share code, notes, and snippets.

@Foxpace
Last active July 23, 2023 14:31
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/b27ef8eeec09f3dbe4d8e4a02c47de3f to your computer and use it in GitHub Desktop.
Save Foxpace/b27ef8eeec09f3dbe4d8e4a02c47de3f to your computer and use it in GitHub Desktop.
Test with Turbine and multiple steps
@Test
fun `Given the ViewModel waits - When the event OnLaunch comes, then both computations runs successfully`() =
runTest {
turbineScope {
// ARRANGE
val expectedString = "Result"
heavyComputation.stub {
onBlocking { doComputation() } doAnswer { expectedString }
}
val sut = ExampleViewModel(heavyComputation)
val firstStateReceiver = sut.vmState.testIn(backgroundScope)
val secondStateReceiver = sut.secondVmState.testIn(backgroundScope)
// ACTION
sut.onEvent(VmEvents.OnLaunch)
// CHECK
assertEquals(VmState.Waiting, firstStateReceiver.awaitItem())
assertEquals(VmState.Waiting, secondStateReceiver.awaitItem())
assertEquals(VmState.Running, firstStateReceiver.awaitItem())
assertEquals(VmState.Running, secondStateReceiver.awaitItem())
assertEquals(VmState.Finished(expectedString), firstStateReceiver.awaitItem())
assertEquals(VmState.Finished(expectedString), secondStateReceiver.awaitItem())
assertEquals(VmState.Waiting, firstStateReceiver.awaitItem())
assertEquals(VmState.Waiting, secondStateReceiver.awaitItem())
firstStateReceiver.cancel()
secondStateReceiver.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment