https://kotlinlang.org logo
#kotest
Title
# kotest
c

CLOVIS

09/21/2023, 8:44 PM
I'm trying to access the
TestScope
from KotlinX.Coroutines.test (e.g. to have access to the
backgroundScope
). According to https://kotest.io/docs/framework/coroutines/test-coroutine-dispatcher.html, adding
.config(coroutineTestScope = true)
should be enough to enable it, but I'm not sure how to access it. The only
CoroutineScope
I see is the
ContainerScope
provided in the test, and it doesn't look like it provides those accessors.
s

sam

09/22/2023, 7:59 PM
You can use the extension value
testCoroutineScheduler
c

CLOVIS

09/23/2023, 10:14 AM
That's only the scheduler. I also want the foreground and background scopes.
The option
testScheduler
was renamed to
coroutineTestScope
, so I assume the scope is available somehow?
If I'm reading this correctly, it is instantiated in
TestCoroutineInterceptor.kt:27
but never stored/passed to anything else, so it is just lost 😕
It looks like it's added to the coroutine context as
TestScopeContainer
, but it never reaches the coroutine context of the test for some reason. Even if it did, it doesn't look like
io.kotest.core.coroutines.TestScopeContainer
is available externally? Context of a test in StringSpec:
[RunningInRunTest, kotlinx.coroutines.test.TestCoroutineScheduler@2b4738b0, kotlinx.coroutines.test.CoroutineExceptionHandler$1@2b569700, TestScope[test started], StandardTestDispatcher[scheduler=kotlinx.coroutines.test.TestCoroutineScheduler@2b4738b0]]