【发布时间】:2021-03-24 21:08:50
【问题描述】:
目前我正在编写一个小型演示应用程序,它使用 Ktor 作为其应用程序环境,并使用 Kodein 作为依赖注入框架。
在应用程序初始化期间,我确实导入了一些模块,其中一个是我想在集成测试初始化期间替换的:
fun Application.module(testing: Boolean = false) {
logger.debug { "Starting main" }
restModule()
di {
bind<Json>() with singleton {
Json {
...
}
}
import(persistenceModule)
}
在测试中,我想使用不同的persistenceModule,例如。一个内存模块。我的测试初始化如下:
fun start() {
val configPath = ClassLoader.getSystemResource("application-acceptanceTest.conf").file
engine = embeddedServer(CIO, commandLineEnvironment(arrayOf("-config=$configPath")))
engine.start()
val disposable = engine.environment.monitor.subscribe(ApplicationStarted) { application: Application ->
started = true
}
while (!started) {
Thread.sleep(10)
}
disposable.dispose()
}
我已经试过打电话了
engine.application.di
但这让我(很明显)只能访问已经初始化的 Ktor 功能。有这样的可能吗?
【问题讨论】:
-
在
Application.module的定义中,可以访问属性developmentMode,如果是true,则导入内存模块而不是持久化模块。您可以在此处找到有关启用开发模式的信息ktor.io/docs/development-mode.html#enable