I am trying to test a repository function which re...
# coroutines
l
I am trying to test a repository function which returns Flow<List<item>>. I have a fake repository where I emit some data into MutableSharedFlow using myFlow.tryEmit(items). I wanted to test what happens if you throw exception while emitting into myFlow. How do I do that?
n
You can pass the exception through by "materializing" items/errors as a Results and then unwrapping:
Copy code
val eventResults = MutableSharedFlow<Result<Int>>(extraBufferCapacity = Int.MAX_VALUE)
val events = eventResults.map { it.getOrThrow() }
startCollecting(events)
eventResults.tryEmit(Result.success(5))
eventResults.tryEmit(Result.failure(Exception("Blah")))
or you could merge in a separate error flow
Copy code
val items = MutableSharedFlow<Int>(extraBufferCapacity = Int.MAX_VALUE)
val error = CompletableDeferred<Int>()
val events = merge(items, error::await.asFlow())
startCollecting(events)
items.tryEmit(5)
error.completeExceptionally(Exception("Blah"))
l
Thanks Nick!