Created
September 17, 2023 17:08
-
-
Save khahani/46b01a755936a00982b8fb810512b940 to your computer and use it in GitHub Desktop.
ResultKtTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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