Mock ApolloClient's query(call) for testing purposes

1.8k Views Asked by At

Kotlin Android app using graphQl & ApolloClient.

I want to test this method:

fun myMethod(id: String): Single<List<MyEntity>> {
    val call = Query(id)
    return Rx2Apollo.from(apolloClient.query(call))
      .map { modeCode() }.firstOrError().onErrorReturn { emptyList() }
  }

Is there an elegant way to mock the response of apolloClient.query(call) so Rx2Apollo returns the response?

I tried with a RealApolloCall, with a custom ApolloCall, etc, nothing convinces me. And I wouldn't want to extract and mock the whole Rx2Apollo.from(apolloClient.query(call)) part. To use mockWebServer doesn't apply, because I do not want to test apolloClient, I want to mock its response to test the rest.

There is a related issue that was closed without really mentioning the best way to solve: https://github.com/apollographql/apollo-android/issues/1144

Thanks!

0

There are 0 best solutions below