fossasia / open-event-droidgen

Open Event Android App Generator https://github.com/fossasia/open-event-android/raw/apk/sample-apk-fossasia17-development.apk
GNU General Public License v3.0
2.05k stars 828 forks source link

Write ViewModel Tests #2017

Open iamareebjamal opened 6 years ago

iamareebjamal commented 6 years ago

Actual Behaviour

To be done after #2016 There are no unit tests on common code for ViewModels

Expected Behaviour

Unit Test coverage for every ViewModel should aim to be 100%

Parent Issue : #2014

dr0pdb commented 6 years ago

@iamareebjamal How would you recommend writing unit tests for those ViewModels which use Realm ? Should we mock the realm database ?

Right now the constructor of these ViewModel initialises the Realm instance For eg.

public TrackSessionsActivityViewModel() {
        realmRepo = RealmDataRepository.getDefaultInstance();
}

We can't create a mock Realm in-memory instance to test this ViewModel. I think we should introduce dependency injection in the project so that we could pass the Realm Instance as a parameter in the Constructor and then only we could test our ViewModels by mocking the Realm Database inside our tests.

iamareebjamal commented 6 years ago

Right now it'll require a lot of effort to do so. Create a package protected method to set RealmRepo and set mock implementation through it

iamareebjamal commented 6 years ago

Also, let's focus on pre-assigned issues first, because we need to move away from maintenance mode to feature development mode. We are very behind in implementing the API

dr0pdb commented 6 years ago

Ok, will do this after those issues.

iamareebjamal commented 6 years ago

@srv-twry When you implement one such PR for a test case for a ViewModel, then reopen the issue and link since it is required to have an aligning issue with PR, and this issue cannot be solved in one PR.