Koin: checkModules() test failed for scoped definitions and viewModel

Created on 21 May 2019  路  4Comments  路  Source: InsertKoinIO/koin

Describe the bug
Application works, but test checkModules() fails.

To Reproduce
Clone repo and run KoinDependenciesTest: Repo, branch with robolectric

Expected behavior
Test pass

Koin project used and used version (please complete the following information):
Koin Version = "2.0.0-GA6"

Given
Roboelectric test for Koin application

@RunWith(RobolectricTestRunner::class)
@Config(application = MyApplication::class, sdk = [23])
class KoinDependenciesTest : AutoCloseKoinTest() {

    @Test
    fun `check Koin definitions`() {
        getKoin().checkModules()
    }
}

Koin application

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@MyApplication)
            androidLogger()
            modules(myModule)
        }
    }
}

val myModule = module {
    scope(named<MainActivity>()) {
        scoped<Service> { MyService() }
    }
    viewModel { (scope: Scope) -> MyViewModel(scope.get()) }
}

Service and View model

class MyViewModel(private val service: Service) : ViewModel() {
    fun loadDataForUi() = service.getDataFromRepository()
}

interface Service {
    fun getDataFromRepository(): String
}

class MyService : Service {
    override fun getDataFromRepository() = "My service"
}

When
I run check Koin definitions test

Then
I have exception:
org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'com.example.koinchecktestfail.MyViewModel']
Caused by: org.koin.core.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.DefinitionParameters@4a9d794

Error is thrown in checkMainDefinitions(allParameters) when try to resolve MyViewModel dependency from the rootScope with named<MainActivity>() scoped MyService.

P.S. Perhaps there is a better way to use Koin + ViewModel + LifeCycleOwner + scopes?

Most helpful comment

In version 2.0.0 problem solved and now i can do like this:

val myModule = module {
    scope(named<MainActivity>()) {
        scoped<Service> { MyService() }
        viewModel { MyViewModel(service = get()) }
    }
}

then use it in activity:

private val model: MyViewModel by currentScope.viewModel(this)

And now test check Koin definitions works fine.

Thanks for good product )

All 4 comments

It is noteworthy that if run the unit test without robollectic (Repo, main branch), he does not check anything. because Koin has no definitions.

class KoinDependenciesTest : AutoCloseKoinTest() {

    @Test
    fun `check Koin definitions`() {
        startKoin { myModule }.checkModules()
    }
}

If stop debug inside private fun Koin.checkMainDefinitions(allParameters: MutableMap<CheckedComponent, ParametersCreator>):

rootScope.beanRegistry.getAllDefinitions() = {HashSet@1081}  size = 0
scopeRegistry.getScopeSets() = {ConcurrentHashMap$ValuesView@1086}  size = 0

yeah, forgot to use modules(myModule) instead

Hi, Giuliani

yeah, forgot to use modules(myModule) instead

Yes, if replace
startKoin { myModule }.checkModules() -> startKoin { modules(myModule) }.checkModules() test works as expected and fail with the same exception like test with Robolectric.

But what with this failed test? @arnaudgiuliani

In version 2.0.0 problem solved and now i can do like this:

val myModule = module {
    scope(named<MainActivity>()) {
        scoped<Service> { MyService() }
        viewModel { MyViewModel(service = get()) }
    }
}

then use it in activity:

private val model: MyViewModel by currentScope.viewModel(this)

And now test check Koin definitions works fine.

Thanks for good product )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dakuenjery picture dakuenjery  路  4Comments

pchmielowski picture pchmielowski  路  3Comments

Jeevuz picture Jeevuz  路  4Comments

sankarsana picture sankarsana  路  4Comments

mubarak1361 picture mubarak1361  路  3Comments