InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.98k stars 710 forks source link

Verify graph; Missing definition for inlined dependency #1714

Closed timrijckaert closed 2 months ago

timrijckaert commented 10 months ago

Describe the bug I'm trying to verify my graph where I have a lot of dependencies that should not be exposed on the graph. Instead some dependencies are inlined in the constructor.

It seems koin-test does not take this into account and fails

Verifying module 'org.koin.core.module.Module@5125a460' ...

|-> definition [Singleton:'be.persgroep.ClassB'] | bind types: [class be.persgroep.ClassB] | 1 dependencies to check

  • ----- > Missing definition type 'be.persgroep.ClassA' in definition '[Singleton:'be.persgroep.ClassB']' Fix your Koin configuration or add extraTypes parameter to whitelist the type: verify(extraTypes = listOf(be.persgroep.ClassA::class))

Expected behavior Test should pass as it should take into account inlined dependencies.

Koin module and version: koin-core:3.5.0

Snippet or Sample project to help reproduce

class CheckKoinGraph {
    @Test
    fun debug() {
        module {
            single {
                B(A())
            }
        }.verify()
    }
}

class A
class B(private val a: A)
timrijckaert commented 10 months ago

These test should be added to the VerifyModulesTest

@Test
fun verify_inlined_dependencies_with_get() {
    val module = module {
        single { Simple.ComponentA() }
        single { Simple.ComponentC(Simple.ComponentB(get())) }
    }

    try {
        module.verify()
    } catch (e: MissingKoinDefinitionException) {
        fail("Should not fail to verify module - $e")
    }
}

@Test
fun verify_inlined_dependencies() {
    val module = module {
        single { Simple.ComponentC(Simple.ComponentB(Simple.ComponentA())) }
    }

    try {
        module.verify()
    } catch (e: MissingKoinDefinitionException) {
        fail("Should not fail to verify module - $e")
    }
}

@Test
fun verify_inlined_dependency() {
    val module = module {
        single { Simple.ComponentB(Simple.ComponentA()) }
    }

    try {
        module.verify()
    } catch (e: MissingKoinDefinitionException) {
        fail("Should not fail to verify module - $e")
    }
}
stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.