单元测试因 MockResponse() 的错误 (MockKException) 而失败

Unit test fail with Error(MockKException) for MockResponse()

我有一个客户端将向 GraphQL 端点发出请求,如下所示

import com.apollographql.apollo.ApolloClient

internal class GraphQLClient(apolloClient:ApolloClient, retryStrategy:RetryStrategy){

   override fun <D : Operation.Data, T, V : Operation.Variables> mutate(mutate: Mutation<D, T, V>): Flow<T> {
        val response = apolloClient.mutate(mutate)
        val responseFlow = response.toFlow()
        return responseFlow.map<Response<T>, T> {
            it.data ?: throw ResponseError("Data Null")
        }.retryWhen { cause, attempt ->
            retryStrategy.retry(cause,attempt)
        }
    }
}

我正在使用 MockWebServer 来测试上面的内容以创建模拟响应

   @Test
    fun `GIVEN a successful update, THEN don't retry`() = runBlocking {
        val server = MockWebServer()
        val mockResponse = MockResponse()
        //Successful response in json format. It's the correct format.
        mockResponse.setBody(readFileFromResources("mock_success_response.json"))
        mockResponse.setResponseCode(200)
        server.enqueue(mockResponse)
        server.start()

        val url = server.url("http://loalhost:8080")
        val apolloClient: ApolloClient = ApolloClient.builder()
            .okHttpClient(OkHttpClient())
            .serverUrl(url.toString())
            .addCustomAdapters()
            .build()

        val retryStrategy = mockk<RetryStrategy>()
        val graphQLClient = GraphQLClient(apolloClient)

        //The mutation of intrest
        val mutation = UpdateMutation(
            SomeInput(
                "123"
            )
        )

        //note how i haven't mocked anything related to retry strategy cause this test doesn't need that

        graphQLClient.mutate(mutation).test {
            verify(exactly = 0) { retryStrategy.retry(any(),any()) }
            expectComplete()
        }

        server.shutdown()

    }

但是,我的测试失败了 app.cash.turbine.AssertionError: Expected complete but found Error(MockKException)

在堆栈跟踪的下方,我可以看到关于缺少 answer 一些重试逻辑相关事物的抱怨 但我认为这是抛出上述异常的原因,实际上,甚至不应该执行 Caused by: io.mockk.MockKException: no answer found for: RetryStrategy(#1).isError(com.apollographql.apollo.exception.ApolloNetworkException: Failed to execute http call)

P:S- 我可能在这里测试得太多了,但很想了解发生了什么

我尝试过的事情 如果有影响但错误没有变化,只需将响应更改为空字符串。这让我觉得它可能与响应数据无关,

谢谢

问题出在这一行

 val url = server.url("http://loalhost:8080")

MockServer 不需要主机或端口

 val url = server.url("/somepath")