Skip to content

Instantly share code, notes, and snippets.

@shredderskelton
Created February 21, 2021 11:45
Show Gist options
  • Save shredderskelton/d5f971a398d90a7aad8a04d9f0da4f9c to your computer and use it in GitHub Desktop.
Save shredderskelton/d5f971a398d90a7aad8a04d9f0da4f9c to your computer and use it in GitHub Desktop.
Unit Test Hot Flow Take
/**
* Unit Testing Hot Flows the Android recommended way
*/
@Ignore
@Test
fun `hot flow - hangs`() {
runBlocking {
val actual = MutableStateFlow("test").single()
assertThat(actual).isEqualTo("test")
}
}
@Test
fun `hot flow - This job has not completed yet`() {
rule.testDispatcher.runBlockingTest {
val actual = MutableStateFlow("test").single()
assertThat(actual).isEqualTo("test")
}
}
@Test
fun `hot flow first - Success!`() {
runBlocking {
val actual = MutableStateFlow("test").first() // first() cancels the Flow
assertThat(actual).isEqualTo("test")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment