Colton Idle
05/14/2021, 2:08 AM@Test
fun `test network call`() {
var response: Response<Projects>
GlobalScope.async {
response = service.getMostPopularProjects()
}
assertThat(response.body().popularProjectSize().toString()).isGreaterThan("1")
}Zach Klippenstein (he/him) [MOD]
05/14/2021, 2:27 AMGlobalScope. I want to change the name of this channel to #coroutines-no-globalscopeZach Klippenstein (he/him) [MOD]
05/14/2021, 2:28 AMgetMostPopularProjects is a suspend function? Use runBlocking, runBlockingTest, or one of the other coroutine testing facilitiesColton Idle
05/14/2021, 2:29 AMrunBlockingTest, or one of the other coroutine testing facilities"Zach Klippenstein (he/him) [MOD]
05/14/2021, 2:29 AMZach Klippenstein (he/him) [MOD]
05/14/2021, 2:30 AMrunBlocking is probably the simplest answer, but the “right” answer depends on your needs.Colton Idle
05/14/2021, 2:30 AMColton Idle
05/14/2021, 2:31 AMZach Klippenstein (he/him) [MOD]
05/14/2021, 2:32 AMasync, use launchColton Idle
05/14/2021, 2:47 AMtestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3")
and then did this
@Test
fun `test network call`() {
var response: Response<Projects>
runBlocking {
response = service.getMostPopularProjects()
assertThat(response.body().popularProjectSize().toString()).isGreaterThan("1")
}
}
and everything seems to work as expected. No other setup! Thanks Zach!ephemient
05/14/2021, 4:06 AMrunBlocking and aren't setting any test dispatchers or using runBlockingTest, you don't need kotlinx-coroutines-testColton Idle
05/14/2021, 4:42 AM