文档讨论了依赖注入,但并未真正显示它是如何完成的。如何在ktor中注入依赖关系应用
文档未完成以及和有一堆占位: http://ktor.io/getting-started.html
我想在一个方法来创建我的主要功能,它接受的参数(这是我的依赖),但未能上试当我打电话给withTestApplication
。 我看着应用程序代码,看到应用程序接受一个配置对象,但我不知道如何改变该配置对象注入一些依赖关系。
package org.jetbrains.ktor.application
/**
* Represents configured and running web application, capable of handling requests
*/
class Application(val environment: ApplicationEnvironment) : ApplicationCallPipeline() {
/**
* Called by host when [Application] is terminated
*/
fun dispose() {
uninstallAllFeatures()
}
}
/**
* Convenience property to access log from application
*/
val Application.log get() = environment.log
在使用withTestApplication
测试代码我有类似如下的内容:(我需要模拟和注入参数)
@Test
internal fun myTest() = withTestApplication (Application::myMain)
如果我叫myMain
与参数上面withTestApplication
会失败
更新:
的是起诉是,在我的请求处理中,我使用了一个依赖类,连接到外部其他Web服务并执行一些请求,我需要一种方法能够在我的测试中注入它,因此可以对其进行存根/模拟并更改其行为基于我的测试案例。
谢谢你的回应,在我的情况下,我确实有一个与其他服务交互的类依赖关系,我如何注入那个(所以当我编写我的测试时,我只是依赖/传递一个模拟实例而不是真正的) –