Last active
July 3, 2023 20:45
-
-
Save Foxpace/6a9096037999eb9ace9973f9511d75c0 to your computer and use it in GitHub Desktop.
Example test with turbine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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