tseisel
12/01/2019, 1:18 PMtest operator for testing Flow. Here is the current API :
val flow = flow {
emit(0)
emit(1)
emit(2)
delay(200)
emit(3)
delay(500)
throw Exception("Flow failure")
}
// At this point, subscribe to the source flow.
// Elements are collected manually to test various scenarios such as backpressure or triggering listeners.
flow.test { /* this = TestCollector<Int> */
// Expect to receive exactly 1 element right now. It is collected to the "values" list.
// This fails if the flow terminates, throws an exception or the element is not available right now.
expect(1)
assertEquals(0, values[0])
// You can collect multiple elements at once.
expect(2)
assertEquals(1, values[1])
assertEquals(2, values[2])
// When expecting to collect elements within a time delay, use the variant with duration parameter.
// I may replace TimeUnit with the new Duration class from kotlin.time when more stable.
expect(1, 200, TimeUnit.MILLISECONDS)
assertEquals(3, values[3])
// You can also expect exceptions.
val failure = expectFailure()
assertEquals("Flow failure", failure.message)
}
My questions are :
1. Do some of you need this kind of API ? If yes, does the current API satisfy your needs ? What feature would you add ?
2. Is something similar already planned for kotlinx.coroutines.test, or is there any library that already provide this feature ?