google-developer-training / basic-android-kotlin-compose-training-unscramble

Apache License 2.0
98 stars 140 forks source link

Testing ViewModel : How does WordsData.kt gets swapped while testing, without DI? #145

Open pawanharariya opened 12 months ago

pawanharariya commented 12 months ago

URL of codelab https://github.com/google-developer-training/basic-android-kotlin-compose-training-unscramble/blob/main/app/src/main/java/com/example/unscramble/ui/GameViewModel.kt

Describe the problem When the ViewModel is initialised, this currentWord = allWords.random() method is called. allWords lies in WordsData.kt file. The test source set has different implementation of allWords. But how is it getting swapped automatically without any dependency injection? How does ViewModel automatically know that for testing it should use the test file?

When I renamed the file in test source set, the ViewModel was calling the method from main source set during testing. So it is due to the fact that method lies in same file structure? If so? Why bother for DI for swapping fakes such as a fake repository?