https://kotlinlang.org logo
Title
p

Pablo

12/15/2021, 3:23 PM
I have a question guys, I'm trying to test my Presenter which is :
internal class MyPresenter @Inject constructor(...) {
 private val _uiState = MutableStateFlow<Foo>(Foo.Loading)
 override val uiState: StateFlow<Foo> = _uiState
 private val _sideEffect = MutableSharedFlow<Foo2>()
 override val sideEffect : SharedFlow<Foo2> = _sideEffect
}

init {
  scope.launch {
    usecase.getBar().fold(
     onSuccess = { _uiState.emit(mapper(whatever)) 
      if(...) _sideEffect.emit(whatever)
     }
     onFailure = { _uiState.emit(handleError(it)) }
  }
}
I'm trying to handle these emit, but I'm not succeeding I'm trying with the library app.cash.turbine but is throwing me an error...
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms
(Coroutine boundary)
What I'm doing in the test is :
@Test
fun test() {
  //given with the usecase call and willReturn what I want
  //given with the mapper call and willReturn what I want
  //Init presenter so the init {} method gets fired 
  presenter.uiState.test { 
   assertEquals(WhatIExpect, awaitItem())
   awaitComplete()
}
But it's not working, is there something I'm missing?
n

Nick Allen

12/15/2021, 5:24 PM
It looks like you are waiting for uiState to finish. MutableStateFlow never terminates.
Looks like turbine has a
cancelAndIgnoreRemainingEvents()
method you may want instead of
awaitComplete()
.
p

Pablo

12/18/2021, 6:45 PM
I've found a way but looks like I have to create presenter.onInit() inside the
presenter.uiState.test{...}