Skip to content

Dependencies problem: TestMainDispatcher is not set as main dispatcher, have Dispatchers.Main[missing] instead. #242

@MarcinMoskala

Description

@MarcinMoskala

Using Dispatchers.setMain and Dispatchers.Main makes tests fail with:

Error: XXX: TestMainDispatcher is not set as main dispatcher, have Dispatchers.Main[missing] instead.

The simplest code to reproduce this problem:

https://pl.kotl.in/06K0zWkU2

class NewsViewModelTest {
    @Test
    fun test() {
        val dispatcher = StandardTestDispatcher()
        Dispatchers.setMain(dispatcher)
        var a = 0
        CoroutineScope(Dispatchers.Main + SupervisorJob()).launch {
            delay(1000)
            a = 1
        }
        dispatcher.scheduler.advanceUntilIdle()
        assertEquals(1, a)
    }
}

Seems that the coroutines and coroutines-test versions do not match.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions