Marcin Wisniowski
02/20/2019, 9:13 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
, is there a way to replace <http://Dispatchers.IO|Dispatchers.IO>
to use the main thread in unit tests? Or do I have to pass a dispatcher with DI to replace in tests?Melvin Biamont
02/20/2019, 9:30 PMopen class CoroutineContextProvider {
open val Main: CoroutineContext by lazy { Dispatchers.Main }
open val IO: CoroutineContext by lazy { Dispatchers.Default }
}
You can use it like this : GlobalScope.launch(coroutineContextProvider.Main) { (...) }
And for the Unit tests, you can override this class like this :
object MockCoroutineContextProvider : CoroutineContextProvider() {
override val Main: CoroutineContext = Dispatchers.Unconfined
override val IO: CoroutineContext = Dispatchers.Unconfined
}
ghedeon
02/20/2019, 9:36 PMrunBlocking{}
? 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.Marcin Wisniowski
02/20/2019, 9:43 PMrunBlocking{}
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.dewildte
02/20/2019, 10:04 PMMarcin Wisniowski
02/20/2019, 10:33 PMrunBlocking
isn't sufficient in the future.