https://kotlinlang.org logo
#ktor
Title
# ktor
r

Raman Sharma

11/27/2023, 12:47 PM
Hi Everyone, I am trying to write test for ktor client:
Copy code
@Test
fun `getScreenData returns Success with screen data`() = runBlocking {

    val mockScreenId = "test_screen_id"
    val mockScreenData = ScreenJson("test_id", "test_data", "test_type", "test_url")
    val mockHttpClient = mockk<HttpClient>()
    every { networkKtorModule.client } returns mockHttpClient
    coEvery {
        mockHttpClient.get<ScreenJson>("${networkKtorModule.networkConfig.baseUrl}/screens/$mockScreenId")
    } answers {
        val mockResponse = mockk<HttpResponse>()
        every { mockResponse.status } returns HttpStatusCode.OK
        every { mockResponse.receive<String>() } returns mockScreenData
        mockResponse
    }

    val result = apiService.getScreenData(mockScreenId)
}
When trying to mock api call, I am getting Type mismatch.:
Copy code
coEvery {
        mockHttpClient.get<ScreenJson>("${networkKtorModule.networkConfig.baseUrl}/screens/$mockScreenId")
} Required: MockKMatcherScope.DynamicCall Found: HttpResponse Any help will be appreciated.
a

Aleksei Tirman [JB]

11/27/2023, 12:51 PM
Unfortunately, that not gonna work because the
HttpClient.get
is an inline method. To mock the response you can use the MockEngine.
1
r

Raman Sharma

11/27/2023, 1:00 PM
Ok thanks