fuel icon indicating copy to clipboard operation
fuel copied to clipboard

How to test or mock responseObject()

Open matteosimone opened this issue 3 years ago • 2 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

matteosimone avatar Jan 13 '22 17:01 matteosimone

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?

kittinunf avatar Jan 26 '22 07:01 kittinunf

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

matteosimone avatar Jan 26 '22 16:01 matteosimone