apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.74k stars 653 forks source link

Allow deserializing models with json files in tests #653

Closed scottkennedy closed 7 years ago

scottkennedy commented 7 years ago

I'm trying to write some unit tests where I need the model objects, but there doesn't seem to be a way of doing this without setting up a mock server that takes in a request, which is a lot of overhead for a unit test.

digitalbuddha commented 7 years ago

I'm not sure I understand what you are trying to achieve or what is lacking?

sav007 commented 7 years ago

You can try to replicate the logic inside server response body parser https://github.com/apollographql/apollo-android/blob/master/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/HttpResponseBodyParser.java#L36

where you replace response.body().source() with json string

mandrizzle commented 7 years ago

@sav007 What about the case where I don't want to mock an entire query? For example, what if I want to just want to deserialize some fragment with some JSON and use that in my test? Is there any way to do that?

digitalbuddha commented 7 years ago

Can you use mockito?

On Sep 11, 2017 8:31 PM, "Mandeep Singh" notifications@github.com wrote:

@sav007 https://github.com/sav007 What about the case where I don't want to mock an entire query? For example, what if I want to just want to deserialize some fragment with some JSON and use that in my test? Is there any way to do that?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/apollographql/apollo-android/issues/653#issuecomment-328696318, or mute the thread https://github.com/notifications/unsubscribe-auth/AEUX3Dw9_syrDoU9RDiNbZy_yh0q7B9mks5shdDLgaJpZM4PRh9H .

mandrizzle commented 7 years ago

Mocking a JSON model is a lot of effort, I would rather use JSON and deserialize my models from that

sav007 commented 7 years ago

@mandrizzle any model generated by Apollo including fragments, has a Mapper class, that can be used to deserialize from JSON. It has one method map(ResponseReader) where response reader can be custom implementation that backed by string json.

scottkennedy commented 7 years ago

Do you have any example of a customer reader?

sav007 commented 7 years ago

I don't have right now, but will come up with smth later this week

sidgrover21 commented 3 years ago

Guys thanks a lot for this. You've saved a me a lot of effort in my unit tests.