Hello! I've been creating unit tests for verifying...
# koin
f
Hello! I've been creating unit tests for verifying some Koin modules by mocking the dependencies needed for each module using *declareMock*() and verifying the modules using *checkModules*() (basically following along with https://insert-koin.io/docs/reference/koin-test/testing and https://insert-koin.io/docs/reference/koin-test/checkmodules), which has been fine so far as the scope for the dependencies have been either single or factory. I have however run into a scoped dependency which is scoped using the Ktor plugin RequestScope (org.koin.ktor.plugin.RequestScope), which I haven't been able to mock in a similar fashion. Does anyone know how to mock a scoped (specifically scope<RequestScope>) dependency similar to how you would mock a singleton or a factory?
Copy code
class SomeDependencyA

data class SomeDependencyB(
    val someA: SomeDependencyA
)

class SomeKoinModuleTest : KoinTest {

    @Suppress("unused")
    @JvmField
    @RegisterExtension
    val koinTestRule = KoinTestExtension.create {

        val testModule = module {
            scope<RequestScope> {
                scoped<SomeDependencyB> {
                    SomeDependencyB(get<SomeDependencyA>())
                }
            }
            
            single<SomeDependencyB>(named("SomeSingletonB")) { 
                SomeDependencyB(get<SomeDependencyA>())
            }
        }

        modules(
            testModule
        )
    }

    @Suppress("unused")
    @JvmField
    @RegisterExtension
    val mockProvider = MockProviderExtension.create { clazz ->
        mockkClass(clazz)
    }

    private fun declareMocks() {
        declareMock<SomeDependencyB>(named("SomeSingletonB"))
        // What's the equivalent for declaring mock for the scope<RequestScope> definition?
    }

    @Test
    fun verifyKoinModule() {
        declareMocks()

        getKoin().checkModules {}
    }
}
m
Hello Felix, It might be a limitation of current API. A work around would consist in overloading with a definition using the mock directly. best
👍 1