【问题标题】:How can I check Koin modules while using injection parameters?如何在使用注入参数时检查 Koin 模块?
【发布时间】:2019-05-09 16:51:34
【问题描述】:

我想使用koin-test 提供的checkModules() 方法检查我的配置,如here 所述。

但是,我使用的是injection parameters,但我的测试失败并出现异常:

org.koin.core.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.DefinitionParameters@3804648a

这里有一个简单的测试来证明这个问题:

import org.junit.Test
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.check.checkModules

class TestCase : KoinTest {
    @Test
    fun checkModules() {
        koinApplication {
            modules(
                module { factory { (msg: String) -> Message(msg) } }
            )
        }.checkModules()
    }

    data class Message(val message: String)
}

有没有办法让它工作?我怎样才能提供缺少的参数?

【问题讨论】:

    标签: kotlin koin


    【解决方案1】:

    您需要将此参数传递给您的测试,如下所示:

    class TestCase : KoinTest {
        @Test
        fun checkModules() {
            koinApplication {
                modules(module { factory { (msg: String) -> Message(msg) } })
            }.checkModules {
                create<Message> { parametersOf("testMessage") }
            }
        }
    
    
        data class Message(val message: String)
    }
    

    来自 Koin 存储库的示例:CheckModulesTest.kt#L156

    我遇到同样的问题:Issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多