<https://github.com/Kotlin/kotlinx.coroutines/issu...
# coroutines
s
https://github.com/Kotlin/kotlinx.coroutines/issues/1365 I'm curious if anyone has implemented a solution to this issue besides constructor injection of IO dispatcher?
For context, I have an abstract class that contains several methods that launch coroutines with
<http://Dispatchers.IO|Dispatchers.IO>
and switch to
withContext(Dispatchers.Main)
inside. I would rather not constructor inject the IO Dispatcher inside every class that is implementing my abstract class, or do something like pass the IO Dispatcher as a parameter in all my public functions. Not sure if there is a way to mock this; open to suggestions! (Please!)
s
There's indeed currently only a
setMain
to inject your own (test) dispatcher. I inject the dispatchers into my dependencies, but if you want to avoid that, you could create your own singleton in your code that uses the actual Dispatchers.Xxx per default but can be configured to return custom (test) dispatchers. This would be a bit error prone, though. Easy to forget and accidentally use the standard Dispatchers.Xxx instead....