https://kotlinlang.org logo
j

jean

09/15/2021, 7:16 AM
I’m following what KaMPKit wrote in this stackoverflow thread to test my sqldelight queries https://stackoverflow.com/questions/65663436/how-to-write-unit-tests-for-sqldelight-on-kmm My test succeeds in android but I get
kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one.
when running the same test for ios. this is my BaseTest class in
iosTest
Copy code
actual abstract class BaseTest {
    @OptIn(DelicateCoroutinesApi::class)
    actual fun <T> runTest(block: suspend CoroutineScope.() -> T) {
        var error: Throwable? = null
        GlobalScope.launch(Dispatchers.Main) {
            try {
                block()
            } catch (t: Throwable) {
                error = t
            } finally {
                CFRunLoopStop(CFRunLoopGetCurrent())
            }
        }
        CFRunLoopRun()
        error?.also { throw it }
    }
}
I just copied what they have. Any idea what I’m doing wrong?
a

andylamax

09/15/2021, 8:18 AM
Don't use Dispatchers.Main in tests
j

jean

09/15/2021, 8:57 AM
Thanks 🙂 Using
Dispatchers.Default
instead seems to work
o

Osman Saral

09/15/2021, 11:49 AM
came here to ask same question 😄 i'm lucky i guess
a

andylamax

09/15/2021, 12:41 PM
yo welcome
l

leandro

09/15/2021, 7:09 PM
you need to use the multithreaded version of coroutines, e.g.
1.5.1-native-mt
2 Views