0

我的ViewModel需求repositorygenre通过构造函数。repository由 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) injection inactivity 按预期工作,但由于找不到参数,试运行测试失败key.genre检查完整的错误日志

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

完整的应用程序源

4

1 回答 1

0

正如Arnaud Giuliani在推特上指出的那样。dryRun接受 lambda 函数作为参数

class KoinDryRunTest : KoinTest {
    @Test
    fun dependencyGraphDryRun() {
        startKoin(list = appModules)
        dryRun() {
            mapOf("key.genre" to "dummy string")
        }
    }
}
于 2018-08-17T05:42:02.030 回答