Last active
August 14, 2020 15:41
-
-
Save douglasiacovelli/36171d7404de2eb987cf234b299e97ac to your computer and use it in GitHub Desktop.
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
class NetworkHelperTest { | |
private val dispatcher = TestCoroutineDispatcher() | |
@Test | |
fun `when lambda returns successfully then it should emit the result as success`() { | |
runBlockingTest { | |
val lambdaResult = true | |
val result = safeApiCall(dispatcher) { lambdaResult } | |
assertEquals(ResultWrapper.Success(lambdaResult), result) | |
} | |
} | |
@Test | |
fun `when lambda throws IOException then it should emit the result as NetworkError`() { | |
runBlockingTest { | |
val result = safeApiCall(dispatcher) { throw IOException() } | |
assertEquals(ResultWrapper.NetworkError, result) | |
} | |
} | |
@Test | |
fun `when lambda throws HttpException then it should emit the result as GenericError`() { | |
val errorBody = "{\"errors\": [\"Unexpected parameter\"]}".toResponseBody("application/json".toMediaTypeOrNull()) | |
runBlockingTest { | |
val result = safeApiCall(dispatcher) { | |
throw HttpException(Response.error<Any>(422, errorBody)) | |
} | |
assertEquals(ResultWrapper.GenericError(422, ErrorResponse(listOf("Unexpected parameter"))), result) | |
} | |
} | |
@Test | |
fun `when lambda throws unknown exception then it should emit GenericError`() { | |
runBlockingTest { | |
val result = safeApiCall(dispatcher) { | |
throw IllegalStateException() | |
} | |
assertEquals(ResultWrapper.GenericError(), result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment