Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatchers.Main in androidUnitTest kmm #419

Closed
droideveloper opened this issue May 18, 2024 · 4 comments
Closed

Dispatchers.Main in androidUnitTest kmm #419

droideveloper opened this issue May 18, 2024 · 4 comments

Comments

@droideveloper
Copy link

is there a way to make PlatformMainDispatcher actual for android instead of Dispatcher.Main.immediate to Dispatchers.Main
would not let me mockk Dispachers.Main 馃

@DevSrSouza
Copy link
Collaborator

Dispatchers.setMain does not work for the case?

@droideveloper
Copy link
Author

droideveloper commented May 21, 2024

well actually on each before I did set Dispatchers.setMain(UnconfinedTestDispatcher())
I do have dependencies in build.gradle.kt

        androidUnitTest.dependencies {
            implementation(libs.coroutines.core)
            implementation(libs.coroutines.android)
            implementation(libs.coroutines.test)
            implementation(libs.mockk)
        }

but when run tests it fails with HandlerDispatcher etc error, so I was wondering immediate usage of it could be reason for this ? thanks.

@droideveloper
Copy link
Author

droideveloper commented May 21, 2024

and if I use it like this in @BeforeEach

mockkStatic("cafe.adriel.voyager.core.model.ScreenModelKt")
every { screenModel.screenModelScope } returns testScope

and testScope is

private val testScope = TestScope(UnconfinedTestDispatcher())

I could run my tests

@droideveloper
Copy link
Author

droideveloper commented May 25, 2024

We went with option to provider func and provide TestScope in our tests, for anyone has same issue

class FooScreenModel(
    private val scope: ScreenModel.() -> CoroutineScope = { screenModelScope },
) : StateScreenModel<FooState>(FooState())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants