Created
February 21, 2021 11:36
-
-
Save shredderskelton/4b3a9cb8bed9224b29560364b60b3e39 to your computer and use it in GitHub Desktop.
Unit Test Cold Flow Take
This file contains hidden or 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
/** | |
* 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