architecture-samples
architecture-samples copied to clipboard
[dev-hilt] Mocking AddEditTaskViewModel instead of using FakeRepository in AddEditTaskFragmentTest
All the UI tests in the dev-hilt branch are using a fake implementation of TasksReporitory
to indirectly control the behavior of ViewModels. This approach works because the same instance of FakeRepository
is injected into both AddEditTaskFragmentTest
and AddEditTaskViewModel
. In other words, FakeRepository
is implemented as a singleton.
What I want to achieve is to mock the AddEditTaskViewModel
instead of using FakeRepository
. As AddEditTaskViewModel
is private in AddEditTaskFragment
there is no easy way to mock the AddEditTaskViewModel.
The closest thing I've found is in this post, but IMHO the solution provided is not a clean one.
I had the same opinion. However, Dagger Hilt seems to recommend using the actual dependencies. So I want to know why this repo use fake Repository instead of API or DB. https://dagger.dev/hilt/testing-philosophy