11package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase14
22
33import com.lukaslechner.coroutineusecasesonandroid.mock.mockAndroidVersions
4+ import com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency.scope
45import junit.framework.Assert.assertEquals
56import kotlinx.coroutines.cancel
67import kotlinx.coroutines.launch
@@ -33,36 +34,37 @@ class AndroidVersionRepositoryTest {
3334 }
3435
3536 @Test
36- fun `loadRecentAndroidVersions() should continue to load and store android versions when calling scope gets cancelled` () = runTest {
37- val fakeDatabase = FakeDatabase ()
38- val fakeApi = FakeApi ()
39- val applicationScope = this
40- val repository = AndroidVersionRepository (
41- fakeDatabase,
42- applicationScope,
43- api = fakeApi
44- )
37+ fun `loadRecentAndroidVersions() should continue to load and store android versions when calling scope gets cancelled` () =
38+ runTest {
39+ val fakeDatabase = FakeDatabase ()
40+ val fakeApi = FakeApi ()
4541
46- // Sharing the testScheduler with the applicationScope is important!
47- val viewModelScope = TestScope ( this .testScheduler)
48- val job = viewModelScope.launch {
49- repository.loadAndStoreRemoteAndroidVersions()
50- }
42+ val repository = AndroidVersionRepository (
43+ database = fakeDatabase,
44+ scope = this ,
45+ api = fakeApi
46+ )
5147
52- // execute coroutine until delay(1) in the fakeApi
53- applicationScope.runCurrent()
48+ // Sharing the testScheduler with the applicationScope is important!
49+ val viewModelScope = TestScope (this .testScheduler)
50+ val job = viewModelScope.launch {
51+ repository.loadAndStoreRemoteAndroidVersions()
52+ }
5453
55- // Check if nothing is inserted into the db before we cancel the scope
56- assertEquals( false , fakeDatabase.insertedIntoDb )
54+ // execute coroutine until delay(1) in the fakeApi
55+ runCurrent( )
5756
58- // Cancel the scope and check if it was indeed cancelled
59- viewModelScope.cancel()
60- assertEquals(true , job.isCancelled)
57+ // Check if nothing is inserted into the db before we cancel the scope
58+ assertEquals(false , fakeDatabase.insertedIntoDb)
6159
62- // continue coroutine execution after delay(1) in the fakeApi
63- applicationScope.advanceTimeBy( 1 )
64- applicationScope.runCurrent( )
60+ // Cancel the scope and check if it was indeed cancelled
61+ viewModelScope.cancel( )
62+ assertEquals( true , job.isCancelled )
6563
66- assertEquals(true , fakeDatabase.insertedIntoDb)
67- }
64+ // continue coroutine execution after delay(1) in the fakeApi
65+ advanceTimeBy(1 )
66+ runCurrent()
67+
68+ assertEquals(true , fakeDatabase.insertedIntoDb)
69+ }
6870}
0 commit comments