Magnus Gudmandsen
09/12/2023, 2:44 PMmockServerTest
block in a very minimal test:
mockServerTest {
mockServer.enqueue(mockResponse)
MyService(apolloClient).getData()
}
However, I just get a UncompletedCoroutinesError
(test coroutine not completing). The getData()
function is a suspend function that just calls apolloClient.query(MyQuery).execute()
, and returns the result.Stylianos Gakis
09/12/2023, 2:49 PMMagnus Gudmandsen
09/12/2023, 2:50 PMmockServerTest
blockMagnus Gudmandsen
09/12/2023, 2:52 PMval mockServer = MockServer()
val apolloClient = ApolloClient.Builder()
.serverUrl(mockServer.url())
.build()
mbonnin
09/12/2023, 2:55 PMMagnus Gudmandsen
09/12/2023, 2:56 PMmbonnin
09/12/2023, 2:56 PMMagnus Gudmandsen
09/12/2023, 2:56 PMmbonnin
09/12/2023, 2:58 PMMagnus Gudmandsen
09/12/2023, 2:58 PMMagnus Gudmandsen
09/12/2023, 2:59 PMStylianos Gakis
09/12/2023, 2:59 PMrunTest
somewhere in there?
I see that at one place I am using it, I am doing this https://github.com/HedvigInsurance/android/blob/41758c154b9483826864a5f67f0f301caa[…]54/app/app/src/test/kotlin/com/hedvig/app/apollo/ApolloUtils.kt but I don’t remember if I needed that or not. Might be unrelated though, don’t want to steer you too far from your original issue.Stylianos Gakis
09/12/2023, 3:00 PMrunTestCoroutineLegacy
in there, are you using some old coroutine dependency with the old runBlockingTest which is now deprecated?Magnus Gudmandsen
09/12/2023, 3:00 PMmockServerTest
block calls kotlin's runTest
internally, yesMagnus Gudmandsen
09/12/2023, 3:02 PMkotlinCoroutines = "1.7.3"
mbonnin
09/12/2023, 3:04 PMStylianos Gakis
09/12/2023, 3:04 PMMagnus Gudmandsen
09/12/2023, 3:16 PMrunApolloTest
function, and it definitely seems to work better! Now I'm just getting Failed to parse GraphQL http network response
, but that's on me 😅
I'll get back to you to verify once this works all the way through.
In the meanwhile: Thanks for the help! 🙏Magnus Gudmandsen
09/12/2023, 3:17 PMTestDispatcher
being supplied in your case that makes the difference? 🤔mbonnin
09/12/2023, 3:24 PMmockServer
was mostly intended for internal uses, we should probably hide it or rework the API a bitmbonnin
09/12/2023, 3:24 PMkotlinx.coroutines
allow you to inject a test dipatcher IIRC?Magnus Gudmandsen
09/12/2023, 3:25 PMfun runApolloTest(
block: suspend CoroutineScope.(MockServer, ApolloClient) -> Unit,
) {
return kotlinx.coroutines.test.runTest {
val mockServer = MockServer()
val apolloClient = ApolloClient.Builder()
.serverUrl(mockServer.url())
.build()
block(mockServer, apolloClient)
apolloClient.close()
mockServer.stop()
}
}
Stylianos Gakis
09/12/2023, 3:28 PMMagnus Gudmandsen
09/12/2023, 3:36 PMStylianos Gakis
09/12/2023, 3:41 PMMagnus Gudmandsen
09/13/2023, 7:34 AM__typename
in the body for fragments... fun times!
Now everything works, data is parsed, and the test passes!
Thanks a lot for the help 🙂