英文:
Using Koin for Ktor-Client in Android: Missing type 'io.ktor.client.engine.HttpClientEngine'
问题
Here's the translated code portion:
我不明白为什么这个用于 ktor 客户端的模块会失败 -
```kotlin
fun provideKtorClient() = HttpClient(OkHttp) {
install(ContentNegotiation) {
json()
}
}
val ktorModule = module {
single { provideKtorClient() }
}
@Test
fun checkKtorModule() {
ktorModule.verify()
}
一个简单的测试失败,错误信息是 -
org.koin.test.verify.MissingKoinDefinitionException: 在定义中缺少类型 'io.ktor.client.engine.HttpClientEngine' 用于类 'io.ktor.client.HttpClient'
我尝试按照错误消息创建了一个 HttpClientEngine
提供程序。然而,这导致了更多的对象和提供程序的深入追踪。我印象中 ktor 的 OkHttp
等对象不应该需要进一步的初始化。这让我觉得我可能在这里漏掉了一些更大的东西?谢谢!
编辑 / 回答
在编写简单的单元测试时,需要提供额外的类型以进行初始化,如下所示 -
@Test
fun checkKtorModule() {
ktorModule.verify(
extraTypes = listOf(
io.ktor.client.engine.HttpClientEngine::class,
io.ktor.client.HttpClientConfig::class
)
)
}
如果运行 Android 的仪器测试,这些类可以正常加载,无需其他规格 -
@Test
fun checkAllModules() {
koinApplication{
modules(appModule, ktorModule)
checkModules(){
withInstance<Ordering.Context>()
withInstance<Application>()
withInstance<SavedStateHandle>()
}
}
}
感谢所有的帮助!
<details>
<summary>英文:</summary>
I could use some help understanding why this module for a ktor client fails -
fun provideKtorClient() = HttpClient(OkHttp) {
install(ContentNegotiation) {
json()
}
}
val ktorModule = module {
single { provideKtorClient() }
}
@Test
fun checkKtorModule() {
ktorModule.verify()
}
A simple test fails with the error -
`org.koin.test.verify.MissingKoinDefinitionException: Missing type 'io.ktor.client.engine.HttpClientEngine' for class 'io.ktor.client.HttpClient' in definition '\[Singleton:'io.ktor.client.HttpClient'\]'`
I tried creating an `HttpClientEngine` provider following the error message. However, this led to chasing even more objects and providers down the rabbit hole. My impression was that objects like ktor's `OkHttp` shouldn't need any further initialization. This makes me think I'm missing something larger here? Thanks!
### EDIT / Answer
When writing a simple unit test, the extra types need to be provided for initialisation like so -
@Test
fun checkKtorModule() {
ktorModule.verify(
extraTypes = listOf(
io.ktor.client.engine.HttpClientEngine::class,
io.ktor.client.HttpClientConfig::class
)
)
}
If running an Android instrumentation test, these classes load fine with no other specification needed -
@Test
fun checkAllModules() {
koinApplication{
modules(appModule, ktorModule)
checkModules(){
withInstance<Ordering.Context>()
withInstance<Application>()
withInstance<SavedStateHandle>()
}
}
}
Thanks for all the help!
</details>
# 答案1
**得分**: 1
我已经通过为 `HttpClient` 的构造函数提供依赖项使其工作,但我不确定这是否是正确的方法。
```kotlin
fun provideKtorClient() = HttpClient {
install(ContentNegotiation) {
json()
}
}
val ktorModule = module {
single { provideKtorClient() }
single<HttpClientEngine> { OkHttp.create() }
single<HttpClientConfig<OkHttpConfig>> { HttpClientConfig() }
}
@Test
fun checkKtorModule() {
ktorModule.verify()
}
英文:
I've made it work by providing the dependencies for the HttpClient
's constructor but I'm not sure if this is a right way to do it.
fun provideKtorClient() = HttpClient {
install(ContentNegotiation) {
json()
}
}
val ktorModule = module {
single { provideKtorClient() }
single<HttpClientEngine> { OkHttp.create() }
single<HttpClientConfig<OkHttpConfig>> { HttpClientConfig() }
}
@Test
fun checkKtorModule() {
ktorModule.verify()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论