How to correctly mock ViewModel on androidTest

后端 未结 4 2108
野性不改
野性不改 2021-02-13 03:21

I\'m currently writing some UI unit tests for a fragment, and one of these @Test is to see if a list of objects is correctly displayed, this is not an integ

4条回答
  •  遥遥无期
    2021-02-13 03:39

    I have solved this problem using an extra object injected by Dagger, you can find the full example here: https://github.com/fabioCollini/ArchitectureComponentsDemo

    In the fragment I am not using directly the ViewModelFactory, I have defined a custom factory defined as a Dagger singleton: https://github.com/fabioCollini/ArchitectureComponentsDemo/blob/master/uisearch/src/main/java/it/codingjam/github/ui/search/SearchFragment.kt

    Then in the test I replace using DaggerMock this custom factory using a factory that always returns a mock instead of the real viewModel: https://github.com/fabioCollini/ArchitectureComponentsDemo/blob/master/uisearchTest/src/androidTest/java/it/codingjam/github/ui/repo/SearchFragmentTest.kt

提交回复
热议问题