Skip to content

使用相依注入進行測試 ​

必要相依性: io.ktor:ktor-server-di

程式碼範例: server-di

相依注入 (DI) 外掛程式提供工具來簡化測試。

你可以在載入應用程式模組之前覆寫相依性:

kotlin
fun test() = testApplication {
  application {
    dependencies.provide<MyService> {
      MockService()
    }
    loadServices()
  }
}

在上述範例中,loadServices() 是用於引導應用程式模組的函式——例如註冊路由與服務的函式,相當於 application.yaml 中 modules 下所列出的內容。

在測試中載入配置 ​

使用 configure() 可在測試中輕鬆載入配置檔案:

kotlin
fun test() = testApplication {
  // 從預設配置檔案路徑載入屬性
  configure()
  // 載入多個檔案並進行覆寫
  configure("root-config.yaml", "test-overrides.yaml")
}

測試引擎會忽略衝突的宣告,以便讓你自由覆寫。