architecture-samples icon indicating copy to clipboard operation
architecture-samples copied to clipboard

[dev-hilt] Mocking AddEditTaskViewModel instead of using FakeRepository in AddEditTaskFragmentTest

Open MasoudFallahpour opened this issue 4 years ago • 1 comments

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.

MasoudFallahpour avatar Jul 12 '20 05:07 MasoudFallahpour

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

takahirom avatar Sep 24 '20 05:09 takahirom