TBD54566975 / tbdex-kt

Apache License 2.0
5 stars 3 forks source link

Write server / handler unit tests to ensure callbacks are being invoked #223

Open jiyoonie9 opened 7 months ago

jiyoonie9 commented 7 months ago

Maybe using mockk library will help

jiyoonie9 commented 7 months ago

Did most of this work in https://github.com/TBD54566975/tbdex-kt/pull/218/commits/1fa47a5347dd9aa1253e90d21966a3d32db0a2fe

However, there's an issue testing any handler methods that invoke ApplicationCall#receiveText() like CreateExchange and SubmitMessage. Mocking this is proving difficult.

Example test written (currently not being run)

fun `verify callback is invoked upon successful rfq submission`() = runBlocking {
      // todo ApplicationCall.receiveText() is an extension function
      //  need to find another way to mock it
      coEvery { applicationCall.parameters["exchangeId"] } returns close.metadata.exchangeId
      coEvery { applicationCall.receiveText() } returns Json.stringify(close)

      coVerify(exactly = 1) { submitClose(applicationCall, exchangesApi, any<SubmitCloseCallback>(), close)}

    }

Example error stack:

SubmitMessageTest > SubmitMessageMockkTest > verify callback is invoked upon successful rfq submission() FAILED
    io.ktor.server.plugins.CannotTransformContentToTypeException: Cannot transform this request's content to io.ktor.utils.io.ByteReadChannel
        at app//io.ktor.server.request.ApplicationReceiveFunctionsKt.receiveNullable(ApplicationReceiveFunctions.kt:107)
        at app//tbdex.sdk.httpserver.handlers.SubmitMessageTest$SubmitMessageMockkTest$verify callback is invoked upon successful rfq submission$1$2.invokeSuspend(SubmitMessageTest.kt:117)
        at app//tbdex.sdk.httpserver.handlers.SubmitMessageTest$SubmitMessageMockkTest$verify callback is invoked upon successful rfq submission$1$2.invoke(SubmitMessageTest.kt)
        at app//tbdex.sdk.httpserver.handlers.SubmitMessageTest$SubmitMessageMockkTest$verify callback is invoked upon successful rfq submission$1$2.invoke(SubmitMessageTest.kt)...