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

Pablo

10/07/2020, 9:33 AM
I have already ask this here but is still causing me overhead, if on my test presenter I'm creating a val dispatcher = TestCoroutineDispatcher() and a TestCoroutineScope(dispatcher) is there any difference if I do this? And I create the presenter as : presenter=MyPresenter(..,testscope,testdispatcher) runBlockingTest{ Here my presenter creates a launch{} } dispatcherTest.runBlockingTest{ Here my presenter creates a launch{} } scopeTest.runBlockingTest{ Here my presenter creates a launch{} } I've read the documentation and inside of the runBlockingTest is creating a TestCoroutineScope() but then it's not aligned or it's not the same as the presenter? So if the presenter takes longer or fails what will happen? This api creates a TestCoroutineScope() to act as a parent of the one is in the block? Because the thing is that I want the presenter (the dispatcher or scope) to avoid fails on the CI because maybe on my local machine passes but in a slow machine it doesn't wait until the end and fails. Any idea?
g

gildor

10/07/2020, 10:30 AM
you should create presenter from your runBlockingTest
to share dispatcher
runBlockingTest{ createPresenter(this) } fun createPresenter(scope: CoroutineScope) = MyPresenter(..,scope, scope.coroutineContext[CoroutineDispatcher]!!)
p

Pablo

10/07/2020, 12:16 PM
So in the onBefore I put this? And what is the advantage to do this? What happens if I do it like I said, create private val of TestCoroutineScope and TestDispatcher and pass these val on the constructor on before and then on each test use runBlockingTest, won't be aligned the coroutines then? If the presenter does something extra work it may not wait until it ends??
g

gildor

10/07/2020, 12:58 PM
No, I suggest to do this in test, see snippet
What is "be aligned the coroutines"? My snippet above uses scope provided by runBlockingTest
p

Pablo

10/07/2020, 1:06 PM
But then, on each test how do I use runBlockingTest? Using an extension function of TestDispatcher testscope or without it?
g

gildor

10/07/2020, 2:31 PM
on each test how do I use runBlockingTest
What do you mean?
Copy code
@Test fun someTest() = runBlockingTest{
 val presenet = createPresenter()
 assertTrue(presenter.doSomething())
}

fun CoroutineScope.createPresenter() = MyPresenter(..,this, coroutineContext[CoroutineDispatcher]!!)
p

Pablo

10/07/2020, 2:53 PM
So I create the presenter everytime? Or I can do it in onBefore
g

gildor

10/07/2020, 11:26 PM
Yes, every time, so every test will create presenter in own scope btw, even if you do this onBefore it also created every time, by default test class is recreated for every test, so