-
Notifications
You must be signed in to change notification settings - Fork 173
/
RepositoryTest.kt
42 lines (35 loc) · 1.41 KB
/
RepositoryTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.android.coroutines.testing
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
// [START coroutine_test_repo_dispatcher_injection_test]
class RepositoryTest {
@Test
fun repoInitWorksAndDataIsHelloWorld() = runTest {
val dispatcher = StandardTestDispatcher(testScheduler)
val repository = Repository(dispatcher)
repository.initialize()
advanceUntilIdle() // Runs the new coroutine
assertEquals(true, repository.initialized.get())
val data = repository.fetchData() // No thread switch, delay is skipped
assertEquals("Hello world", data)
}
}
// [END coroutine_test_repo_dispatcher_injection_test]
class BetterRepositoryTest {
// [START coroutine_test_repo_dispatcher_injection_test_better]
@Test
fun repoInitWorks() = runTest {
val dispatcher = StandardTestDispatcher(testScheduler)
val repository = BetterRepository(dispatcher)
repository.initialize().await() // Suspends until the new coroutine is done
assertEquals(true, repository.initialized.get())
// [START_EXCLUDE]
val data = repository.fetchData()
assertEquals("Hello world", data)
// [END_EXCLUDE]
}
// [END coroutine_test_repo_dispatcher_injection_test_better]
}