Closed dumbfingers closed 3 years ago
I haven't used navigation much, but what launchInHiltContainer
does is starts your fragment in a basically empty Hilt activity. It is possible you might need more configuration on your activity, so if that is the case, you would want to define your own Hilt activity with the extra navigation logic and use that in your test with ActivityScenario
.
For mocking ViewModels, this is intentionally not supported. This is similar to how you can't really mock a fragment. You can use the various testing APIs though to replace the bindings underlying your ViewModel. If you really feel strongly about mocking your ViewModel logic, you would need to wrap it behind an abstraction that uses the ViewModel as an implementation detail, and then fake/mock it at that abstraction layer.
First thanks for providing Hilt, it is a very good and neat solution for Android development.
I have two questions for using Hilt in UITests:
launchInHiltContainer
methods to launch the current fragment for testing, navigation controller cannot launch another fragment from the current oneby viewmodels()
, how to mock the viewmodel/inject fake one in fragment?My setup:
Followed the guide in https://dagger.dev/hilt/instrumentation-testing to have a custom runner and also
HiltExt
(https://developer.android.com/training/dependency-injection/hilt-testing#launchfragment)Example Fragment A and the dialog both use the same viewmodel for data sharing.
Then during the test:
In the UITest, it has no effect when using the button to launch the dialog