Skip to content

Instantly share code, notes, and snippets.

@khahani
Created September 17, 2023 17:08
Show Gist options
  • Select an option

  • Save khahani/46b01a755936a00982b8fb810512b940 to your computer and use it in GitHub Desktop.

Select an option

Save khahani/46b01a755936a00982b8fb810512b940 to your computer and use it in GitHub Desktop.
ResultKtTest
import app.cash.turbine.test
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.assertEquals
class ResultKtTest {
@Test
fun Result_catches_errors() = runTest {
flow {
emit(1)
throw Exception("Test Done")
}
.asResult()
.test {
assertEquals(Result.Loading, awaitItem())
assertEquals(Result.Success(1), awaitItem())
when (val errorResult = awaitItem()) {
is Result.Error -> assertEquals(
"Test Done",
errorResult.exception?.message,
)
Result.Loading,
is Result.Success,
-> throw IllegalStateException(
"The flow should have emitted an Error Result",
)
}
awaitComplete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment