-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coroutine launch not waiting with StandardTestDispatcher on ViewModel #3880
Comments
Why is this a problem? |
When the main is set as I've found this issue when migrating from a old version of coroutines where this exactly scenario worked but now breaks a lot of tests that we have. |
It shouldn't. Tasks that are In fact,
I don't understand the purpose of the test you're showing. If a |
So @Test
fun test() {
assert(viewModel.state.value == "A")
runTest {
advanceUntilIdle()
assert(viewModel.state.value == "B")
}
} Weirdly if I don't use
The tests we have are setting a mock value for a interactor that the I tried to research this issue and couldn't find anyone having this exact problem I had, i also asked in the slack kotlin before opening an issue here but looks like my lack of knowledge about I'll close this now, thanks for the clarification @dkhalanskyjb! |
@dkhalanskyjb The runTest executing any queued launches prior, is this a new change introduced as of 1.7.0?. I was seeing this behavior change as well and I could not find where it was documented otherwise. |
Nope, it was like that from the start. If you see behavioral changes, please find a case when some code behaves differently on different versions of kotlinx-coroutines and report it as a bug. |
I'm not sure it was, I opened this issue here exactly because I was updating the coroutines to a newer version and some tests that worked previously stopped working after the upgrade. |
Ok, thanks, will look into it. |
I see, I have resolved my issue. The original assumption is that the behavior added as a result of addressing #1205 was already in the library. However with it not, many tests propogated asserting correct functionality where it was not. Thank you! This is awesome |
@greggiacovelli, #1205 is already in the library. It's a completely separate issue. |
No I apologize between 1.6.4 and 1.7.3 there were a lot of additions. The most noticable was run test now failing on any coroutine exception. So the codebase I I was working within was experiencing swallowed nullpointerexceptions. As of 1.7.0 many changes seem to have gone in relating to addressing issue 1205. So the tests under 1.6.4 were incorrectly passing. The tests in my code base should have failed |
Describe the bug
If a
ViewModel
has aviewModelScope.launch{}
on theinit{}
, even after settingDispatchers.setMain(StandardTestDispatcher())
the coroutine runs automatically.Provide a Reproducer
The text was updated successfully, but these errors were encountered: