Testing Android ViewModels that use viewModelScope + Molecule may affect other tests #118
mhernand40
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Been playing around with Molecule in my team's Android project by trying to introduce it as an implementation detail of two View Models; one that extends Jetpack's
ViewModeland usesviewModelScope, and another that does not extend Jetpack'sViewModeland accepts anyCoroutineScopevia the constructor.When it came to running the tests for each View Model, the tests passed when each test class was run in isolation. However, when running all the tests in one run, the test class for the View Model that extends Jetpack's
ViewModelruns before the test class for the View Model that is a plain class, causing the latter's tests to fail.It is worth noting the following:
runTest { … }from the Coroutines Test library with the defaultStandardTestDispatcher,viewModelScopeis used for the JetpackViewModelDispatchers.setMain(…)/Dispatchers.resetMain()ViewModelviewModelScopeTestScope(testScheduler)is used for the View Model that does not extend Jetpack'sViewModelDispatchers.setMain(…)/Dispatchers.resetMain()I have reduced the repro down to the following test class (no Jetpack
ViewModelrequired):test1simulates the scenario when testing a JetpackViewModelthat usesviewModelScope.All reactions