您将如何在 ktor 中使用 koin 设置测试。所以我的意思是我想测试我的 ktor 应用程序,例如我的路线...我想在 Koin 的帮助下借助依赖注入来完成。当我尝试启动 koin 并设置我的测试模块时,我收到错误消息,指出 koin 应用程序已在运行。但我真的不知道该怎么办?我的测试类看起来像下面的代码。我看过 philip lakner 的教程,他在 withApplication 和 handlerrequest 的帮助下设置了一个测试,但没有任何效果,我即将在这个疯狂的测试中发疯。我的意思是,当测试实际上比创建服务器本身更困难时,我开始想知道为什么要这样做......是否有一种更简单的方法可以使用 koin 和假存储库在 Ktor 中设置测试?
package com.PapperSeller.routes
import com.PapperSeller.di.testModule
import com.PapperSeller.repository.user.FakeUserRepository
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import org.koin.core.context.GlobalContext.startKoin
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.inject
import kotlin.test.BeforeTest
import kotlin.test.Test
internal class UserRoutesKtTest : KoinTest{
private val testUserRepository by inject<FakeUserRepository>()
@BeforeTest
fun setup(){
startKoin{
modules(module { single { testModule } })
}
}
@Test
fun `Create user, no body attached, responds with BadRequest`() = testApplication{
application {
}
}
}
我最近也遇到了这个问题,我通过关闭开发模式解决了这个问题。
申请{
发展=假
}