scope.async { scope.testBody() } deferred.getCompletionExceptionOrNull() ?. let { throw it } scope.cleanupTestCoroutines() if (hasActiveJobs()) { throw UncompletedCoroutinesError("Test finished with active jobs: $endingJobs") } } Our test to run
deferred = scope.async { scope.testBody() } dispatcher.advanceUntilIdle() deferred.getCompletionExceptionOrNull() ?. let { throw it } scope.cleanupTestCoroutines() if (activeJobs()).isNotEmpty()) { throw UncompletedCoroutinesError("Test finished with active jobs") } } Setup Run Test
deferred = scope.async { scope.testBody() } dispatcher.advanceUntilIdle() deferred.getCompletionExceptionOrNull() ?. let { throw it } scope.cleanupTestCoroutines() if (activeJobs()).isNotEmpty()) { throw UncompletedCoroutinesError("Test finished with active jobs") } } Setup Run Test Cleanup
= mockFlow() val items: List<Data> = flowOfData.toList() locations shouldContainAll listOf(location1, location2, location3) } Better way to collect from Flows?
expectItem() shouldBeEqualTo item1 expectItem() shouldBeEqualTo item2 } } RepoTest.kt Run: app.cash.turbine.AssertionError: Expected complete but found Item(Data(…)) DataRepoTests should get data
{ stateFlow.test { expectItem() shouldBe UIState.Success expectComplete() } } ! Timed out waiting for 1000 ms kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms