【发布时间】:2023-04-07 18:52:02
【问题描述】:
我正在玩弄一个小型 ktor webapp,我想在多个模块中拆分功能。 我有一个根模块,我可以在其中安装我想在整个应用程序中使用的功能
fun Application.rootModule(testing: Boolean = false) {
install(ContentNegotiation) {
gson {
}
}
....
以及我正在实现域功能的域模块
fun Application.bookModule() {
routing {
get("/books/{id}") {
....
}
}
}
现在我想对此功能进行测试
class BookControllerTests: StringSpec({
"get should return correct book"{
withTestApplication({bookModule()}) {
val testCall: TestApplicationCall = handleRequest(method = HttpMethod.Get, uri = "/books/42") {
}
testCall.response.status() shouldBe HttpStatusCode.OK
}
}
})
如果我这样运行它,我会收到错误消息Response pipeline couldn't transform 'class org.codeshards.buecherkiste.book.domain.Book' to the OutgoingContent - 所以内容协商不起作用。这是有道理的,因为它安装在此处未调用的根模块中。我通过将根模块和域模块包装在一个沿着我的测试用例实现的测试模块中解决了这个问题:
fun Application.bookModuleTest() {
rootModule()
bookModule()
}
现在这似乎奏效了。
由于我在 ktor 和 kotest 方面是个菜鸟,所以我想征求对此解决方案的反馈。这是做我想做的事的正确方法,还是我让自己陷入困境?有没有更好的解决方案?
【问题讨论】: