Skip to content

Cannot figure out how to upgrade test to 1.6.0 #3138

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

Closed
connyduck opened this issue Jan 13, 2022 · 5 comments
Closed

Cannot figure out how to upgrade test to 1.6.0 #3138

connyduck opened this issue Jan 13, 2022 · 5 comments
Assignees
Labels

Comments

@connyduck
Copy link

I have a test for an Android ViewModel that uses androidx.paging. Basically it mocks the Api and then asserts that the Pager.flow returns the expected items.

I tried following the Migration guide, but I cannot get it to work. runTest instead of runBlocking, is no problem. Both StandardTestDispatcher and UnconfinedTestDispatcher seem to do something different from what TestCoroutineDispatcher is doing, however. I also tried inserting advanceUntilIdle(), runCurrent() or advanceTimeBy() in between steps, to no avail.

Here is the test in question: https://github.com/tuskyapp/Tusky/blob/develop/app/src/test/java/com/keylesspalace/tusky/components/timeline/TimelineViewModelTest.kt

Any tips on how I can get this test, and maybe even the disabled one, to work with 1.6 and without deprecations is highly appreciated.

@dkhalanskyjb
Copy link
Collaborator

Looks like a duplicate of #3120. You do collectLatest here, which means that, if the execution order is changed, the result may also change because of new emissions canceling the block, which seems to be the root cause.

@connyduck
Copy link
Author

Yes seems similar.

I spent way too much time on this now, I tried collectLatest and other suggestions from #3120, but the only thing that makes a difference is the dispatcher. Giving up and sticking with TestCoroutineDispatcher for now.

@dkhalanskyjb dkhalanskyjb self-assigned this Jan 17, 2022
@NitroG42
Copy link

Be careful as the TestCoroutineDispatcher seems to behave differently between 1.5.2 and 1.6.0 versions, see here : https://github.com/NitroG42/Coroutine160Test

@dkhalanskyjb
Copy link
Collaborator

dkhalanskyjb commented Jan 17, 2022

@NitroG42, we tried very hard for TestCoroutineDispatcher not to be changed, and I was quite surprised by your discovery, so I tried copy-pasting the test module from 1.5.2 into your project and running the test with it: https://github.com/dkhalanskyjb/Coroutine160Test/commit/5bf782673f694cf75f40783d95e940be7c38d372 The only place where the project uses the new test module is in Dispatchers.(re)setMain. The test still fails, so I don't think TestCoroutineDispatcher is the culprit here.

I think your example shows the problem with TestCoroutineDispatcher quite nicely: the tests that only work with TestCoroutineDispatcher are so brittle that even tiny changes to the implementations of the primitives will break the tests. Some other change in 1.6.0 must be causing the test to fail, though I can't say which one.

@NitroG42
Copy link

No problem, I wrote this sample before understanding the last posts of #3120 .
It was hard at first because I basically expected that when my viewmodel would emit a value in a state flow, it would automatically call the test code with Turbine and awaitItem().
After some struggling I understand better now that if the same scenario would happens in the realworld, well the behavior would be exactly than the 1.6.0 version (conflated value).

@qwwdfsad qwwdfsad closed this as completed Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants