使用Koin在Android中进行Ktor-Client:缺少类型 ‘io.ktor.client.engine.HttpClientEngine’。

huangapple go评论101阅读模式
英文:

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 &#39;io.ktor.client.engine.HttpClientEngine&#39; for class &#39;io.ktor.client.HttpClient&#39; in definition &#39;\[Singleton:&#39;io.ktor.client.HttpClient&#39;\]&#39;`

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&#39;s `OkHttp` shouldn&#39;t need any further initialization. This makes me think I&#39;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&lt;Ordering.Context&gt;()
                withInstance&lt;Application&gt;()
                withInstance&lt;SavedStateHandle&gt;()
            }
        }
    }
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&lt;HttpClientEngine&gt; { OkHttp.create() }
    single&lt;HttpClientConfig&lt;OkHttpConfig&gt;&gt; { HttpClientConfig() }
}

@Test
fun checkKtorModule() {
    ktorModule.verify()
}

huangapple
  • 本文由 发表于 2023年6月8日 11:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定