How can I simulate a no network situation with `Mo...
# ktor
s
How can I simulate a no network situation with
MockEngine
?
a
Think you can just throw an appropriate exception when forming a response:
Copy code
MockEngine {
    throw java.net.ConnectException()
}
There is a drawback that ktor client doesn't wrap exceptions so you should write its own tests for all engines that you use.
s
Well I would need to work out what exception would be thrown then. Right?
👌 2
g
Regarding the 3 years-old response suggesting this approach to simulate "no network" case
Copy code
MockEngine {
    throw java.net.ConnectException()
}
Given MockEngine doesn't allow specifying the coroutine dispatcher, is there an approach where I can simulate this case for all the platforms in an elegant manner?
s
Wouldn't having an
expect
error type and then using
actual typealias
to specify each platforms exceptions work?
g
It could. I ended up using a new HttpClient not mocked to ensure I get a connection exception and a timeout one, using a trick on local IPs. I don't know if it's trully multiplatform yet tho, also it could not work if your local configuration matches this, but the low probability of this event is probably good enough for me.
👍 1
289 Views