Search code examples
androidkotlinviewmodelandroid-architecture-componentskoin

How to provide parameters in Koin dry run test?


My ViewModel needs repository & genre through constructor. repository is provided by Koin & genre string is provided from activity

// Main app module
val MovieListModule: Module = applicationContext {

    // provide repository
    bean {
        DummyMovieListRepository() as MovieListRepository
    }

    // provides ViewModel
    viewModel { params: ParameterProvider ->
        MovieListViewModel(respository = get(), genre = params["key.genre"])
    }
}

//Module list for startKoin()
val appModules = listOf(MovieListModule)

//in activity
val viewModel = getViewModel<MovieListViewModel> {
   mapOf("key.genre" to "Action / Drama")
}

// dry run test which fails
class KoinDryRunTest : KoinTest {
    @Test
    fun dependencyGraphDryRun() {
        startKoin(list = appModules)
        dryRun()
    }
}

// some error log 
org.koin.error.MissingParameterException: Parameter 'key.genre' is missing
    at org.koin.dsl.context.ParameterHolder.get(ParameterHolder.kt:46)
org.koin.error.BeanInstanceCreationException: Can't create bean Factory[class=io.github.karadkar.popularmovies.MovieListViewModel, binds~(android.arch.lifecycle.ViewModel)] due to error :
    org.koin.error.MissingParameterException: Parameter 'key.genre' is missing

here Koin (v 0.9.3) injection inactivity works as expected but the dry run test fails as it can't find parameter key.genre. Check full error-log

Is there any way to mock/provide key.genre value to dry run test?

full app source


Solution

  • as Arnaud Giuliani pointed on twitter. dryRun accepts lambda function for parameters

    class KoinDryRunTest : KoinTest {
        @Test
        fun dependencyGraphDryRun() {
            startKoin(list = appModules)
            dryRun() {
                mapOf("key.genre" to "dummy string")
            }
        }
    }