I'm using Turbine library to test flows. It's work...
# coroutines
k
I'm using Turbine library to test flows. It's working really well for most use cases but this one got my head scratching.
private val _uiState: MutableStateFlow<UIState> = MutableStateFlow(UIState())
val uiState: StateFlow<UIState> = _uiState
private val _isActive: MutableStateFlow<Boolean> = MutableStateFlow(false)
val isActive: StateFlow<Boolean> = _isActive.onStart {
// SOME LOGIC THAT WILL UPDATE uiState flow meaning UIState object
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(ABC_THRESHOLD.inWholeMilliseconds),
initialValue = true,
)
My question is how can I write a test for this scenario: 1. When
isActive
flow is started then
UIState
object has changes.
z
I don’t have an answer for your question, but this design looks problematic. Why are these two separate flows at all?
j
You also don't need Turbine to test
StateFlow
. You can make a change, and then assert on
.value
directly.
🙌 1
k
@Zach Klippenstein (he/him) [MOD] hmm, maybe there's a better way to do it, my use case is as follows: 1. UiState - Flow 1 -> Has the UiState object that displays data / loading / error etc. 2. isActive - Flow 2 -> Updates UiState periodically based on time, say every 10 seconds, change
time
value in the UIState. 3. Flow 3 -> Calls API every 30 seconds and this leads to updating of api data in
UIState
. Therefore, kept them separate and this led to testing challenges. I will try the approach shared by @jw for this scenario, so I can still keep the logic in separate flows and be able to test it too. But keen to hear if there are better ways to do this. 🙏
z
Why do you need a separate flow just to update UiState? Flows are for outputing values to consumers, if you have other work to do just put that in a normal coroutine.
k
The two other flows have an infinite while loop and they do something every x and y seconds respectively in the onStart. I guess, will have to launch them in a separate coroutine inside onStart of uistate flow. Will give this a shot.