I'm heavily using `wire mock` in unit tests. Now I...
# multiplatform
u
I'm heavily using
wire mock
in unit tests. Now I'm looking at migration to KMP. What do I do with wiremock now? It's a JVM library
m
One option is to only run the test that use wiremock on the JVM and just trust that it works on both platforms. If you are using ktor for the crossplatform http client, then it has a testing engine to accomplish similar behavior as wiremock. https://ktor.io/docs/client-testing.html I haven't used either tool, but I think the test engine should work for you.
u
oo nice
anyidea how "real" is it when compared to wiremock?
m
Certainly less real, since it will be using a different engine and not do any real IO. But most of that would be testing the engine and not your code.
u
so its more like in memory sqlite driver?
m
I would say it is more like using a testing stub. In in memory sqlite driver still does sql and queries it just skips the IO. I think this skips the HTTP altogether, you get a request object and map it to a response object and skip the http protocol. But you still do all the JSON parsing and handling of the response.
u
https://ktor.io/docs/client-testing.html#test-client im looking at the actual mocking not sure if I like the fact that this will respond with the same regardless of api i call
which kind of defeats the purpose of testing my api client doenst it?
m
The request is passed into the lambda, so you should be able to customize the response to the request. The simple example, just skipped it.
u
I see, but most of my tests are like this
Copy code
@Test {
	wireMockRule.okResponse(api = "GET /api/foo", ""
 		{ ... } 
	""")

	val repository = FooRepository(apiClient, dao)
	repository.sync()

	assertThat(dao.loadData())...
}
i.e. the api call is internal to the thing I'm testing
m
I don't know what that wireMock code is supposed to be, but whatever you are doing in there you do in the mock engine and the rest of the test stays the same. I assume the wire mock is saying when I get this request, respond with this. And you do the same. For more specific help withe the engine you can ask in #C0A974TJ9
u
damn sorry I forgot the important part - in the wiremock you specify the request it's supposed to match & then respond with the canned thing I edited the snippet (it's pseudo code anyways) but yes I specify a inline server basically, i.e. it will error if I send "GET api/bar" - which the ktor's mock engine would not, correct?
m
It's all about how you configure the mock. You can have if the request is GET api/bar return this response, else return an 404 or whatever you want.
u
you mean to examine the
request
from
MockEngine { request ->
?
m
yes
The lambda will be called for each request made during the test. At least that is my assumption.
They really should have a more complicated example
u
gotcha, thanks!