https://kotlinlang.org logo
Title
b

Brais Gabin

03/02/2022, 4:47 PM
I'm using ktor cli. Is there a way to create a fake of
HttpResponse
? I would like to use it to test a function that receives an
HttpResponse
and returns a
Result
. I know that I could use
MockEngine
to create a
HttpResponse
but it feels too much. I'm assuming that
HttpResponse
is something similar as
Response
in
Retrofit
. And there I could use
Response.error()
or
Response.success()
to create fakes.
a

Aleksei Tirman [JB]

03/02/2022, 5:10 PM
Unfortunately, there is no easy way to do it. Here you can find a way of doing it.
b

Brais Gabin

03/02/2022, 5:30 PM
😢 yes I found that but I don't want to use mockk or mockito... I tried to create a fake myself but it's not possible to create a fake of
HttpClientCall
because its constructor is
internal
.
:yes: 1
j

Javier

03/02/2022, 9:57 PM
I had a problem where one of the possible solutions was that
HttpClientCall
was not internal, @Aleksei Tirman [JB] is it possible to change it to public?
a

Aleksei Tirman [JB]

03/03/2022, 7:20 AM
@e5l
e

e5l

03/03/2022, 7:21 AM
Could you log an issue? It's possible to fix before 2.0.0
b

Brais Gabin

03/03/2022, 11:15 AM
_< I make it work with mockk but now my first test needs 2 seconds to finish. I'll open an issue to find a better way,
internal inline fun <reified T : Any> fakeHttpResponse(
    status: HttpStatusCode,
    value: T,
): HttpResponse {
    return mockk {
        every { this@mockk.status } returns status
        coEvery { receive<T>() } returns value
    }
}
j

Javier

03/03/2022, 11:40 AM
Can you share the issue here @Brais Gabin ?
b

Brais Gabin

03/03/2022, 11:41 AM
I'll
j

Javier

03/03/2022, 11:41 AM
Please add the possibility to change the visibility to HttpClientCall too
b

Brais Gabin

03/03/2022, 11:41 AM
Yes, I'll add a link to this converation too
j

Javier

03/03/2022, 11:41 AM
thank you 🙏
e

e5l

03/07/2022, 11:28 AM
Thanks
j

Javier

03/07/2022, 11:28 AM
Additionally, there are internal functions to create those objects too, so maybe the constructor can still be private/internal but those functions should be public instead
your welcome!