https://kotlinlang.org logo
Title
s

spierce7

03/02/2021, 12:57 AM
What’s the best way to test multiplatform coroutines that have
delay
in them?
a

aleksey.tomin

03/02/2021, 5:11 AM
What do you want to test?
s

spierce7

03/02/2021, 6:22 AM
I have an class that is responsible for ensuring that the app is still logged in. If we don’t talk to the server every x minutes, then we show a message to the user that they need internet to proceed
The logic amounts to a few coroutines with
delay
talking making network calls and updating state. I’m not sure on the best way to test this.
m

Michal Klimczak

03/02/2021, 6:29 AM
Make sure the tested coroutine runs on TestCoroutineDispatcher. It will auto advance the time and if you want to control it manually you have methods for that too - pauseSth and advanceTimeBy
a

aleksey.tomin

03/02/2021, 7:49 AM
You have to test that you make one network call. Delay… If you work with coroutines you can broke this. If you put too much methods in the same thread you will broke delay. But I don’t know how you can test it.