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
-
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
ViewModel
and usesviewModelScope
, and another that does not extend Jetpack'sViewModel
and accepts anyCoroutineScope
via 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
ViewModel
runs 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
,viewModelScope
is used for the JetpackViewModel
Dispatchers.setMain(…)
/Dispatchers.resetMain()
ViewModel
viewModelScope
TestScope(testScheduler)
is used for the View Model that does not extend Jetpack'sViewModel
Dispatchers.setMain(…)
/Dispatchers.resetMain()
I have reduced the repro down to the following test class (no Jetpack
ViewModel
required):test1
simulates the scenario when testing a JetpackViewModel
that usesviewModelScope
.Beta Was this translation helpful? Give feedback.
All reactions