Hi everyone 👋🏼 I want to use a SharedFlow created with the
shareIn
operator +
WhileSubscribed
to cancel/resume the SharedFlow in
repeatOnLifecycle
when the Android App goes to background. It’s working fine so far. I also combine this flow with a MutableStateFlow in my ViewModel to react on user input and update my state.
class MyViewModel(
dataSource: MyDataSource,
sharingStarted: SharingStarted = WhileSubscribed(5000)
) : ViewModel() {
var userInput = MutableStateFlow(1)
val state = dataSource.dataFlow()
.combine(userInput) { data, userInput ->
OutputClass(data, userInput)
}
.shareIn(viewModelScope, sharingStarted, 1)
}
My issue is that I want to test this. I’ve tried to use
Turbine for this but it does not help with the combine.
@Test
fun testMyViewModel() = runBlockingTest {
val cut = MyViewModel(
mockk { coEvery { dataFlow() } returns flowOf(Data()) },
sharingStarted = SharingStarted.Eagerly
)
cut.state.test {
// Also an emit to the userInput here does not help
// cut.userInput.emit(2)
assertEquals(OutputClass(...), awaitItem())
}
}
I’m getting a timeout instead of the result.