Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test or mock responseObject() #826

Open
matteosimone opened this issue Jan 13, 2022 · 2 comments
Open

How to test or mock responseObject() #826

matteosimone opened this issue Jan 13, 2022 · 2 comments

Comments

@matteosimone
Copy link

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

@kittinunf
Copy link
Owner

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?

@matteosimone
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants