Hello Ktorians. I’m trying to make a testable vers...
# ktor
d
Hello Ktorians. I’m trying to make a testable version of the example at https://ktor.io/docs/server-create-http-apis.html In order to not use static data for the
customerStorage
I have removed the
application.conf
and changed the startup to
Copy code
fun main() {
    embeddedServer(
        Netty,
        port = System.getenv("PORT")?.toInt() ?: 8080,
        module = { module(customerStorage) }
    ).start(wait = true)
}

fun Application.module(customers: MutableList<Customer>) {
    configureRouting(customers)
    configureSerialization()
}
How can I now use the this new
Application::module
in my tests? At the moment there is
Copy code
class OrderRouteTests {
    @Test
    fun testGetOrder() = testApplication {
        val response = client.get("/order/2020-04-06-01")
        assertEquals(
            """{"number":"2020-04-06-01","contents":[{"item":"Ham Sandwich","amount":2,"price":5.5},{"item":"Water","amount":1,"price":1.5},{"item":"Beer","amount":3,"price":2.3},{"item":"Cheesecake","amount":1,"price":3.75}]}""",
            response.bodyAsText()
        )
        assertEquals(HttpStatusCode.OK, response.status)
    }
}
but I can’t see how to create a
testApplication
block that builds the app with
Application.module(customers: MutableList<Customer>)
Is this the right tack, or maybe I’m barking up the wrong tree?
👍 1
Aha
Copy code
testApplication {
        application {
            module(customerStorage)
        }
        val response = client.get("/order/2020-04-06-01")
        assertEquals(
            """{"number":"2020-04-06-01","contents":[{"item":"Ham Sandwich","amount":2,"price":5.5},{"item":"Water","amount":1,"price":1.5},{"item":"Beer","amount":3,"price":2.3},{"item":"Cheesecake","amount":1,"price":3.75}]}""",
            response.bodyAsText()
        )
        assertEquals(HttpStatusCode.OK, response.status)
    }
StackOverflow isn’t dead yet!
And here is the video with the results

https://youtu.be/CQ-HkC8-VWc

👍 1
thank you color 1