https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

adibfara

10/22/2018, 11:16 AM
is there a way to override Dispatchers.UI on unit tests in android? it's trying to use Looper.getMainLooper() which is a stubbed method
g

gildor

10/22/2018, 11:35 AM
Do not use Dispatcher.UI directly, instead pass it as dependency of your coroutine scope
a

adibfara

10/22/2018, 11:41 AM
I'm not using it, it's because of this line:
internal val mainHandler = Looper.getMainLooper().asHandler(async = true)
in the
HandlerDispatcher.kt
in coroutines-android
which is a top level variable
z

Zach Klippenstein (he/him) [MOD]

10/25/2018, 7:22 PM
Your test subject shouldn't hard-code
Dispatchers.UI
. You should inject a dispatcher so you can pass a different one in from your unit tests.
6 Views