File tree Expand file tree Collapse file tree
app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase10 Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
33import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44import com.lukaslechner.coroutineusecasesonandroid.utils.ReplaceMainDispatcherRule
55import kotlinx.coroutines.test.StandardTestDispatcher
6+ import kotlinx.coroutines.test.UnconfinedTestDispatcher
7+ import kotlinx.coroutines.test.advanceTimeBy
68import kotlinx.coroutines.test.runCurrent
79import kotlinx.coroutines.test.runTest
810import org.junit.Assert
@@ -22,15 +24,16 @@ class CalculationInBackgroundViewModelTest {
2224
2325 @Test
2426 fun `performCalculation() should perform correct calculations` () = runTest {
27+
28+ val testDispatcher = UnconfinedTestDispatcher (testScheduler)
2529 val viewModel =
26- CalculationInBackgroundViewModel (StandardTestDispatcher (testScheduler) ).apply {
30+ CalculationInBackgroundViewModel (testDispatcher ).apply {
2731 observe()
2832 }
2933
3034 Assert .assertTrue(receivedUiStates.isEmpty())
3135
3236 viewModel.performCalculation(1 )
33- runCurrent()
3437
3538 Assert .assertEquals(
3639 UiState .Loading ,
@@ -45,7 +48,6 @@ class CalculationInBackgroundViewModelTest {
4548 receivedUiStates.clear()
4649
4750 viewModel.performCalculation(2 )
48- runCurrent()
4951
5052 Assert .assertEquals(
5153 UiState .Loading ,
@@ -60,7 +62,6 @@ class CalculationInBackgroundViewModelTest {
6062 receivedUiStates.clear()
6163
6264 viewModel.performCalculation(3 )
63- runCurrent()
6465
6566 Assert .assertEquals(
6667 UiState .Loading ,
You can’t perform that action at this time.
0 commit comments