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

Pablo

10/01/2020, 7:26 AM
Also on 2019 on Android Dev Summit '19 said that we should ALWAYS inject Dispatchers to make the dispatcher different from production as in the unit test. Is still like this?
g

gildor

10/01/2020, 7:31 AM
it’s an opinion. It’s indeed good advice in general, but you pay for it by more complex set up and additional code
p

Pablo

10/01/2020, 7:33 AM
Totally agree. If you don't inject, what do you do on Test?
g

gildor

10/01/2020, 7:35 AM
what do you mean “what do you do on test”?
most of cases perfectly testable with any dispatcher
if api designed to be testable
but if you just have some class that launches some background job and doesn’t allow to await it somehow, you are in trouble, so have to inject test dispatcher instead
but also for cases if you need time advance feature, you have to use test dispatcher, so you also should somehow inject it to class, but it doesn’t required for all cases (“ALWAYS”)
👍 1
p

Pablo

10/01/2020, 8:11 AM
So if I inject the
TestCoroutineScope
and the
TestcoroutineDispatcher
i don't need to use the
Dispatchers.setMain(TestCoroutineDispatcher())
and
Dispatchers.resetMain()
right? Because I'm setting it on my constructor
g

gildor

10/01/2020, 8:23 AM
yes
and it’s the only option for other dispatchers
p

Pablo

10/01/2020, 8:31 AM
I see, because I was creating a rule for testing, but then what you are saying to me now is that if I create this
TestCoroutineScope
and
TestCoroutineDispatcher
via constructor on my rule I don't need this
Copy code
@Beforeall
fun setUp(){
Dispatchers.setMain(TestCoroutineDispatcher())
}

@afterEach
fun afterEach(){
testScope.cleanUpTestCoroutines()
}

@AfterAll
fun tearDown(){
Dispatchers.resetMain()
}
Right?
g

gildor

10/01/2020, 9:17 AM
you also can do testScope.context[CoroutineDispatcher]!!
to get dispatcher associated with scope
you shouldn’t create a new dispatcher every time
p

Pablo

10/01/2020, 3:21 PM
But my question is, does it really need a Rule for this? If I'm injecting the
CoroutineScope
and the
CoroutineDispatcher
does it helps? No, right? I mean, if I do inject, and in the test create the
TestCoroutineScope
and the
TestCoroutineDispatcher
and create the presenter on this case passing that as a parameter, I don't need anything else, right? Or do I have to
cleanUpTestCoroutines()
in the
tearDown
? What I mean is, I created the Rule but now that you've explained to me that
Dispatchers.setMain(..)
and
Dispatchers.resetMain()
doesn't change the
IO
it's not necessary to do it anymore, even if I'm creating the
TestCoroutineDispatcher
right? So when creating the test I just need, create these
TestCoroutineX
and that's it?
g

gildor

10/01/2020, 3:28 PM
rule is really useful to replace main dispatcher, if you inject it, I don’t see reason to have a rule and would just use runBlockingTest
p

Pablo

10/01/2020, 3:31 PM
And last question, is it necessary to
cleanupTestCoroutines()
?
g

gildor

10/01/2020, 3:36 PM
This checks ensures that no coroutines are running on test scope (which may be a bug), runBlockingTest does this automatically after lambda is invoked
p

Pablo

10/01/2020, 3:37 PM
Awesome, thanks Andrey 😉
Have a good day!
g

gildor

10/01/2020, 3:37 PM
Night 🛏️ 😄
6 Views