Skip to content

Instantly share code, notes, and snippets.

@shredderskelton
Created February 21, 2021 11:36
Show Gist options
  • Save shredderskelton/4b3a9cb8bed9224b29560364b60b3e39 to your computer and use it in GitHub Desktop.
Save shredderskelton/4b3a9cb8bed9224b29560364b60b3e39 to your computer and use it in GitHub Desktop.
Unit Test Cold Flow Take
/**
* Unit Testing Cold Flows the Android recommended way
*/
@Test
fun `cold flow - take multiple - Android recommended - Success!`() {
runBlocking {
val actual = mutableListOf<String>()
flow {
emit("test")
emit("test")
}.take(2).collect {
actual.add(it)
}
assertThat(actual).containsExactly("test", "test")
}
}
@Test
fun `cold flow - take too little - Android recommended - Success! Oh no!`() {
runBlocking {
val actual = mutableListOf<String>()
flow {
emit("test")
emit("test")
emit("test") // Added an extra emission, simulating an implementation change
}.take(2).collect {
actual.add(it)
}
assertThat(actual).containsExactly("test", "test") // We want this to fail!
}
}
@Test
fun `cold flow - take too much - Android recommended way - Fails!`() {
runBlocking {
val actual = mutableListOf<String>()
flow {
emit("test")
// Deleted the second emission, simulating an implementation change
}.take(2).collect {
actual.add(it)
}
assertThat(actual).containsExactly("test", "test") // We want this to fail!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment