codelab-kotlin-coroutines icon indicating copy to clipboard operation
codelab-kotlin-coroutines copied to clipboard

MainViewModelTest .whenMainClicked_updateTaps has UncompletedCoroutinesError

Open dgoldhirsch opened this issue 3 years ago • 0 comments
trafficstars

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:

Screen Shot 2022-08-26 at 12 12 20 PM

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:

Screen Shot 2022-08-26 at 12 15 45 PM

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.:

Screen Shot 2022-08-26 at 12 30 58 PM

dgoldhirsch avatar Aug 26 '22 16:08 dgoldhirsch