Skip to content

Instantly share code, notes, and snippets.

@douglasiacovelli
Last active August 14, 2020 15:41
Show Gist options
  • Save douglasiacovelli/36171d7404de2eb987cf234b299e97ac to your computer and use it in GitHub Desktop.
Save douglasiacovelli/36171d7404de2eb987cf234b299e97ac to your computer and use it in GitHub Desktop.
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