Skip to content

Instantly share code, notes, and snippets.

@shredderskelton
Created February 21, 2021 12:04
Show Gist options
  • Save shredderskelton/d91870d18750df8eee671c0c9ed46efa to your computer and use it in GitHub Desktop.
Save shredderskelton/d91870d18750df8eee671c0c9ed46efa to your computer and use it in GitHub Desktop.
Cold Flow Turbine
/**
* Unit Testing Cold Flows with Turbine
*/
@Test
fun `cold flow - take multiple - Turbine`() {
runBlocking {
flow {
emit("test")
emit("test")
}.test {
assertThat(expectItem()).isEqualTo("test")
assertThat(expectItem()).isEqualTo("test")
expectComplete()
}
}
}
@Test
fun `cold flow - take multiple - alternative - Turbine`() {
runBlocking {
flow {
emit("test")
emit("test")
}.test {
val actual = cancelAndConsumeRemainingEvents()
assertThat(actual).containsExactly(
Event.Item("test"),
Event.Item("test"),
Event.Complete
)
}
}
}
@Test
fun `cold flow - take too little - Turbine - catches our implementation change!`() {
runBlocking {
flow {
emit("test")
emit("test")
emit("test") // Added an extra emission, simulating an implementation change
}.test {
assertThat(expectItem()).isEqualTo("test")
assertThat(expectItem()).isEqualTo("test")
expectComplete()
}
}
}
@Test
fun `cold flow - take too much - Turbine -- catches our implementation change!`() {
runBlocking {
flow {
emit("test")
// Deleted the second emission, simulating an implementation change
}.test {
assertThat(expectItem()).isEqualTo("test")
assertThat(expectItem()).isEqualTo("test")
expectComplete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment