```java.lang.IllegalStateException: Module with th...
# kotlintest
m
Copy code
java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize
d
The Main dispatcher is a platform dependent dispatcher. I suggest not having a strict dependency on it, instead figure out a way to inject it into the thing you are testing.
That way you can inject
Dispatcher.Unconfined
into your object.
I know in Android at least the
Dispatcher.Main
can not be called from a unit test.
Copy code
object CoroutineTestingScope : CoroutineScope {
    override val coroutineContext = Dispatchers.Unconfined
}
I usually have my object that run suspending functions take
CoroutineScope
as an argument. Then I can inject the testing scope I just described.
Or I make my suspending functions extension functions of
CoroutineScope
.
Then I can test them using my global testing scope in isolation.
I usually never test a function by itself.
I always test my functions indirectly as part of the larger system.