codelab-kotlin-coroutines
codelab-kotlin-coroutines copied to clipboard
MainViewModelTest .whenMainClicked_updateTaps has UncompletedCoroutinesError
The recommended first test case won't work any more:
@Test fun whenMainClicked_updatesTaps() { subject.onMainViewClicked() Truth.assertThat(subject.taps.getValueForTest()).isEqualTo("0 taps") coroutineScope.advanceTimeBy(1000) Truth.assertThat(subject.taps.getValueForTest()).isEqualTo("1 taps") }
Running it as recommended, without any testing context will produce this:

Same bug exists in the finished_code module of the codelab project.
I fixed it by upgrading in coroutines-codelab build.gradle the coroutines_android_version to '1.6.3' and using the new runTest scope:

Also perhaps note that advanceTimeBy is deprecated since 1.6. Per the deprecation message, the recommended replacement is to advance the scheduler and runCurrent, manually, e.g.:
