to use the main thread in unit tests? Or do I have to pass a dispatcher with DI to replace in tests?
m
Melvin Biamont
02/20/2019, 9:30 PM
I use a class which provide the context; and override it for the Unit tests.
So, the class is
Copy code
open class CoroutineContextProvider {
open val Main: CoroutineContext by lazy { Dispatchers.Main }
open val IO: CoroutineContext by lazy { Dispatchers.Default }
}
And for the Unit tests, you can override this class like this :
Copy code
object MockCoroutineContextProvider : CoroutineContextProvider() {
override val Main: CoroutineContext = Dispatchers.Unconfined
override val IO: CoroutineContext = Dispatchers.Unconfined
}
g
ghedeon
02/20/2019, 9:36 PM
did you try with
runBlocking{}
? If I understand it correctly, most of the time you don't even have to worry about the different context down the stream, because it's all suspended and will be synchronous at the end... Other than that yes, injecting via DI seems like a straight forward solution.
👍 1
m
Marcin Wisniowski
02/20/2019, 9:43 PM
I was using
runBlocking{}
but it didn't work, now that you reassured me that it should I found that the error I was getting was actually not because of coroutines and
runBlocking{}
does actually solve this problem... Thanks.
😉 2
d
dewildte
02/20/2019, 10:04 PM
DI is the best solution, but you have to ensure you pass the correct scope/contexts down the whole chain.