如何在使用注入参数时检查 Koin 模块?

nic*_*ico 0 kotlin koin

我想使用这里解释的checkModules()方法来检查我的配置。koin-test

但是,我正在使用注入参数并且我的测试失败并出现异常:

org.koin.core.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.DefinitionParameters@3804648a
Run Code Online (Sandbox Code Playgroud)

下面是一个简单的测试来演示这个问题:

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)
}
Run Code Online (Sandbox Code Playgroud)

有没有办法使这项工作?我怎么能提供缺少的参数?

Vas*_*ili 5

You need to pass this parameter to your test, like this:

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)
}
Run Code Online (Sandbox Code Playgroud)

Example from Koin repository: CheckModulesTest.kt#L156

My issue with the same question: Issue