Karan Sharma
01/08/2025, 10:56 AMprivate 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.Zach Klippenstein (he/him) [MOD]
01/08/2025, 3:03 PMjw
01/08/2025, 6:13 PMStateFlow
. You can make a change, and then assert on .value
directly.Karan Sharma
01/08/2025, 10:37 PMtime
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. 🙏Zach Klippenstein (he/him) [MOD]
01/09/2025, 5:53 PMKaran Sharma
01/09/2025, 10:14 PM