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

Tobi

01/11/2019, 2:54 PM
Hey folks, I am trying to use
io.ktor.client.engine.mock.MockEngine
in my multiplatform project
commonTest
source set in order to test my engine configuration, but I can’t import it 🤔 I added
implementation "io.ktor:ktor-client-mock:$ktor_version"
to my
commonTest.dependencies
closure in the multiplatform
build.gradle
, but it seems like the dependency can’t be resolved. Any ideas?
Just figured out it works somehow in the
jvmTest
source set when adding
io.ktor:ktor-client-mock-jvm:$ktor_version
to the
jvmTest.dependencies
closure. Not really what I wanted, since I’d like to test it for all targets, not just jvm. Furthermore I figured out, I can’t write the test I want to. My idea was to test an
interceptor
I added, which adds an query param to the
defaultRequest
by using the
MockEngine
and checking the correct url is called. But a) I can’t replace the engine of a client and b) even if I could, the interceptor I wrote is part of the
engine
and would have been overwritten if I could use the
MockEngine
🤔
So two questions: 1) How can I write a test using the
MockEngine
in
commonTest
? 2) How would I test the above mentioned interceptor?
s

Saiedmomen

01/11/2019, 4:14 PM
Hey Tobias 1) Try adding all the platform dependencies to corresponding modules. "mock-jvm" to jvm module, "mock-js" to js module and so on 2) MockEngine is intended to mock the network layer and help test components that depend on it not the layer itself. To test the layer itself you need some kind of mock web server. These can help you capture and verify the requests that your actual network layer sends.
t

Tobi

01/11/2019, 4:40 PM
1) Did that. In my case it’s
iOS
and
JVM
. And it works, almost… 🙂 The problem I am facing now is that
runBlocking
is not available, which I use to wait for the result. Afaik this is one of the limitations regarding
Kotlin/Native
and coroutines, so I guess I need to keep the test in the
jvmTest
source set for now 😞 2) That makes sense. Thanks for the explanation 👍
s

Saiedmomen

01/11/2019, 5:41 PM
You're welcome
124 Views