Testing Flows with Turbine
In this section, we will learn how to test Flows using Turbine, which is a third-party library that we can use to test flows in our project.
Hot flows such as SharedFlow
and StateFlow
, as you learned in the previous chapter, emit values even if there are no listeners. They also keep emitting values and do not complete. Testing them is a bit more complicated. You won’t be able to convert these flows to a list and then compare it to the expected values.
To test hot flows and make testing other Flows easier, you can use a library from Cash App called Turbine (https://github.com/cashapp/turbine). Turbine is a small testing library for Kotlin Flow that you can use in Android.
You can use the Turbine testing library in your Android project by adding the following to your app/build.gradle
dependencies:
dependencies {
…
testImplementation 'app.cash.turbine:turbine:0.8.0'...