Hello guys just need some clarification I have thi...
# coroutines
j
Hello guys just need some clarification I have this
unit test
I was testing
hot flow / MutableSharedFlow
inside my
runBlockTest
I need to collect the flows then cancel the job,
launch
return
Job
and
coroutineScope
return a
Unit
and both of them can call
suspending function
If I use both
launch
it will work I get the desired output which is 10, but if I used
coroutineScope
to collect the flows and cancel the using
launch
I get
Job was cancelled
or instead of launch using both
coroutineScope
I get the same error. Why? Also if I use move the delay to first
launch
collection, I get the same prompt error of
Job was cancelled
, I thought I was running parallel execution e.g: like
launch1
after completed
launch2
then cancel the entire
TestCoroutineScope
.
This is my coroutineRule
Copy code
@ExperimentalCoroutinesApi
class MainCoroutineRule(
    val job: Job = Job(),
    private val dispatcher: CoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher(), TestCoroutineScope by TestCoroutineScope(job + dispatcher) {
    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(dispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        cleanupTestCoroutines()
        Dispatchers.resetMain()
    }
}
m