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

Saiedmomen

01/26/2019, 10:08 AM
How can I simulate a no network situation with
MockEngine
?
a

Alexander

01/27/2019, 12:00 PM
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

Saiedmomen

01/27/2019, 2:30 PM
Well I would need to work out what exception would be thrown then. Right?
👌 2
g

Grégory Lureau

03/14/2022, 2:55 PM
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

Saiedmomen

03/21/2022, 12:35 PM
Wouldn't having an
expect
error type and then using
actual typealias
to specify each platforms exceptions work?
g

Grégory Lureau

03/21/2022, 1:27 PM
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
189 Views