kotlin - 如何使用 Koin 测试 Ktor 模块?
问题描述
我有一个安装了 Koin 的模块。我需要模拟一些注入的依赖项来执行测试。我在任何地方都找不到如何正确地做到这一点。你能告诉我怎么做吗?
class AudioFilesHandlerTest : DescribeSpec(), KoinTest {
private val deviceAuthService by inject<DeviceAuthenticationService>() // it should provide mocked object
init {
describe("AudioFilesHandler") {
val audioFilesUri = "/api/audio-files"
describe(audioFilesUri) {
describe("when device is registered") {
val product = createProduct() // Factory for tests
val device = createDevice(product = product)
every { deviceAuthService.challenge(product.productId, device.deviceId) } returns device // this line doesn't work
describe("and transcription or audio params are missing") {
it("should returns 400") {
withApplication(testEnv) {
val call = handleRequest(uri = audioFilesUri, method = HttpMethod.Post) {
val boundary = "WebAppBoundary"
addHeader(ContentType, FormData.withParameter("boundary", boundary).toString())
addHeader("productId", product.productId)
addHeader("deviceId", device.deviceId)
}
call.response.status() shouldBe HttpStatusCode.Unauthorized
}
}
}
}
}
}
}
}
解决方案
推荐阅读
- javascript - Javascript - 如何创建一个对象数组,其中对象的键都具有相同的值
- angular - 如何在不包含角度 6 的情况下从另一个组件调用方法
- java - 无法将应用内产品添加到已发布的应用
- linux - Oracle JDBC 驱动程序是否适用于 Active Directory 身份验证?
- jquery - jQuery 选择一个类的下一个实例
- python - 日期列中的 Python 范围
- c# - 按钮发送命令 SerialPort1_DataReceived (EventArgs e) 发送和接收响应,与不需要它的人产生冲突
- ruby-on-rails - Docker build 说的是无法满足的约束
- angular - AngularJS 6 中的表单
- reporting - Pentaho 报告