Kevin Worth
09/05/2023, 3:28 PMflow.catch
that can offer some pointers?
Given a StateFlow, how can I force my unit test to exercise my .catch
? It appears the unit test framework is designed to suppress all `Exception`s other than those that the test is expecting (i.e. assertThrows
). I don’t want to assert an exception is thrown, I want my flow.catch{ … }
to catch it, but I believe the framework is catching it instead.
val uiState: StateFlow<UiState> = myRepository.myModels
.map<List<MyModel>, UiState>(::Success)
.catch {
// TRYING TO OBSERVE THIS IN TEST
emit(UiState.Error(it))
}
.stateIn(...)
Maybe I’ve gone in the entirely wrong direction?
_
EDIT: Looks like @Test(expected = Exception::class)
is helpful to get passed “Suppressed: Exception”, but now I still have the issue of my .catch
apparently not running because I don’t see an emit.Kevin Worth
09/05/2023, 3:34 PMSuppressed: java.lang.Exception: Simulated error
(because my FakeRepository
is throwing Exception("Simulated error")
)Kevin Worth
09/05/2023, 4:52 PM.catch
apparently not running because I don’t see an emit.
Anyone have any ideas?Kevin Worth
09/05/2023, 5:56 PMflow.catch
never gets the chance to catch it.prudhvi reddy
08/01/2024, 8:21 AM