Skip to content

Commit a196666

Browse files
Improve Unit test for UseCase14
1 parent 69aa8ae commit a196666

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase14/AndroidVersionRepositoryTest.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
55
import junit.framework.Assert.assertEquals
66
import junit.framework.Assert.fail
77
import kotlinx.coroutines.ExperimentalCoroutinesApi
8+
import kotlinx.coroutines.SupervisorJob
9+
import kotlinx.coroutines.cancel
810
import kotlinx.coroutines.launch
11+
import kotlinx.coroutines.test.TestCoroutineScope
912
import kotlinx.coroutines.test.runBlockingTest
1013
import org.junit.Rule
1114
import org.junit.Test
@@ -51,13 +54,14 @@ class AndroidVersionRepositoryTest {
5154

5255
// this coroutine will be executed immediately (eagerly)
5356
// how ever, it will stop its execution at the delay(1) in the fakeApi
54-
val job = launch {
57+
val viewModelScope = TestCoroutineScope(SupervisorJob())
58+
viewModelScope.launch {
5559
println("running coroutine!")
56-
val loadedVersions = repository.loadAndStoreRemoteAndroidVersions()
60+
repository.loadAndStoreRemoteAndroidVersions()
5761
fail("Scope should be cancelled before versions are loaded!")
5862
}
5963

60-
job.cancel()
64+
viewModelScope.cancel()
6165

6266
// continue coroutine execution after delay(1) in the fakeApi
6367
advanceUntilIdle()

0 commit comments

Comments
 (0)