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

Pablo

10/01/2020, 7:21 AM
What is the difference between do
Copy code
TestCoroutineScope.runBlockingTest{}
And
Copy code
TestCoroutineDispatcher.runBlockingTest{}
I see some people use them, and others just put
runBlockingText{}
without any dispatcher nor coroutineScope, is there any difference?
g

gildor

10/01/2020, 7:30 AM
Scope also includes uncaught exception handler
but it also uses TestCoroutineDispatcher
runBlockingTest is just a top level coroutine which uses TestCoroutineScope, so it convinince method
p

Pablo

10/01/2020, 7:35 AM
Still unclear for me Andrey, you mean that if I use testCoroutineScope.runBlockingTest{..} the exceptions wouldn't be caught? Because I saw other coleagues are using both ways and I'm trying to figure it out what's better or efficient. Also I'm used to do it without them, like
Copy code
runBlockingTest {
 my test here
}
g

gildor

10/01/2020, 7:38 AM
just open runBlockingTest implementation, it creates TestCoroutineScope under the hood
also it checks that all jobs started on this scope are finished
the exceptions wouldn’t be caught
no, it’s just records all uncaught exceptions and print stacktrace, just gives a bit more info on crash
actually if you open implementation, all those functions actually do the same, just on different receivers
Top level runBlockingTest is just a good building block, doesn’t require creation of coroutine scope
p

Pablo

10/01/2020, 8:08 AM
That's what I wanted to know 🙂
runBlockingTest
creates everything for you, but I guess they did it like this to ensure, it was running on a TestDispatcher 🤷
g

gildor

10/01/2020, 8:24 AM
but
runBlockingTest
always run on TestDispatchter until you override it
3 Views