Hello. Are there any examples of using `MockEngine...
# ktor
c
Hello. Are there any examples of using
MockEngine
to test a POST request? I'm following the example here (except I'm using
testScope.runTest
to execute the test). However it looks like my request is never executed. If I inspect
mockEngine.requestHistory
immediately after performing the POST I see it is empty.
a
Can you share the code of the test?
c
I did some more digging and found the issue. But I haven't found a solution yet. The issue is not in Ktor - it is in the way I use coroutineScopes.
My code under test is of the form
applicationScope.launch { <http://httpClient.post|httpClient.post>() }
Where
applicationScope
is a separate CoroutineScope that I inject into the class under test. The objective of this is to continue executing the HTTP request even if the calling scope is cancelled (for example even if the user moves away to a different screen) But I'm also executing the test inside a
TestScope
provided by kotlin coroutines test library. So looks like both of them are in the same scope and that ends in the parent scope being cancelled.
If I comment out the
applicationScope.launch
part and directly run my http request in the scope of the calling function, I see some Ktor logs so at least the request is happening.