如何在Koin空运行测试中提供参数?

时间:2018-08-15 13:54:28

标签: android kotlin viewmodel android-architecture-components koin

我的ViewModel通过构造函数需要repositorygenrerepository由Koin提供,genre字符串由活动提供

// 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

此处,Koin(v 0.9.3)的非活动注射按预期工作,但空转测试失败,因为找不到参数key.genreCheck full error-log

有什么方法可以模拟/提供key.genre值以进行空运行测试?

full app source

1 个答案:

答案 0 :(得分:0)

Arnaud Giuliani在Twitter上指出。 dryRun接受lambda函数作为参数

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