android / android/architecture-samples

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

Open
#747 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
45.8k
Forks
11.9k
PR merge metrics
No merged PRs in 30d

Description

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](https://proandroiddev.com/testing-the-untestable-the-case-of-the-viewmodel-delegate-975c09160993), but IMHO the solution provided is not a clean one.

Contributor guide

Open the contributing guide

Research direction

Start with AddEditTaskFragmentTest and AddEditTaskFragment in the dev-hilt branch, then inspect how AddEditTaskViewModel and the singleton FakeRepository are connected. Define a testable ViewModel boundary and update the UI tests to mock AddEditTaskViewModel instead of controlling it through FakeRepository; the issue does not specify the intended design.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile, testing
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.