kittinunf / fuel

The easiest HTTP networking library for Kotlin/Android

Home Page:https://fuel.gitbook.io/documentation/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to test or mock responseObject()

matteosimone opened this issue · comments

Hi,

I have been struggling to figure out how to properly test code that uses fuel. I have reviewed previous issues and people suggest things like:

val someJson = "{\"key\":\"value\"}"
manager.client = mockk<Client>()
every { manager.client.executeRequest(any()).statusCode } returns 200
every { manager.client.executeRequest(any()).data } returns someJson.toByteArray()

This works ok when using responseString(), however when using responseObject(), I get errors like:

Response(child of #2#3), [Failure: no answer found for: Response(child of #2#3).getBody$fuel()
	com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
	com.github.kittinunf.fuel.core.DeserializableKt.response(Deserializable.kt:178)

I have not been able to figure out how to successfully mock getBody or a Response and get it to work. I enjoy using this library but figuring out the testing path has been an obstacle. Do you have any suggestions?

Thanks

Usually, I put Fuel's client under the repository interface and you could supply the mock object in Test. That way you oculd leave the Fuel as real implementation, and mock as a testing implementation.

WDYT? Do you think that is a cleaner approach for testing?

I like and also use that approach however I also worry as a user of an external library if I have used it wrong. There is a good amount of features and also syntactical sugar in the library that I would like to make sure I understand and sometimes the test cases are a good way to exercise the code for myself and future users. I understand that it's probably not the main goal of the library. Just something to think about if there was an easier way to inject responses