TLDR: Anyone have any experience testing `flow.cat...
# flow
k
TLDR: Anyone have any experience testing
flow.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.
Copy code
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.
Test output includes the following in a bunch of stack traces:
Copy code
Suppressed: java.lang.Exception: Simulated error
(because my
FakeRepository
is throwing
Exception("Simulated error")
)
Even after telling the test to expect the exception, I still have the issue of my
.catch
apparently not running because I don’t see an emit. Anyone have any ideas?
…after continuing to look at this… for those playing along at home, I think this really does come down to the intricacies of testing. I believe the test framework is still catching the exception, and simply allowing things to continue at which point my
flow.catch
never gets the chance to catch it.
p
Hey @Kevin Worth You got any workarounds for this?