You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by mhernand40 September 18, 2022
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 uses viewModelScope, and another that does not extend Jetpack's ViewModel and accepts any CoroutineScope 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:
The tests use runTest { … } from the Coroutines Test library with the default StandardTestDispatcher,
TestScope(testScheduler) is used for the View Model that does not extend Jetpack's ViewModel
No usage of Dispatchers.setMain(…)/Dispatchers.resetMain()
I have reduced the repro down to the following test class (no Jetpack ViewModel required):
internalclassRepro {
// This test passes but causes the next test to fail.
@Test
funtest1() {
try {
Dispatchers.setMain(StandardTestDispatcher())
runTest {
val moleculeScope =CoroutineScope(SupervisorJob() +Dispatchers.Main.immediate)
doRunTest(moleculeScope)
// moleculeScope.cancel() // Uncommenting this fixes the test2 failure.
}
} finally {
Dispatchers.resetMain()
}
}
// This test only passes when run by itself or if it runs before test1.// If you rename this to test0 so that it runs before test1, it will pass.
@Test
funtest2() = runTest {
val moleculeScope =TestScope(testScheduler)
doRunTest(moleculeScope)
}
privatefun TestScope.doRunTest(moleculeScope:CoroutineScope) {
val event =MutableSharedFlow<String>(extraBufferCapacity =1)
val state = moleculeScope.launchMolecule(RecompositionClock.Immediate) {
var value by remember { mutableStateOf("") }
LaunchedEffect(event) { event.collect { value = it } }
value
}
runCurrent()
event.tryEmit("test")
runCurrent()
assertEquals("test", state.value)
}
}
test1 simulates the scenario when testing a Jetpack ViewModel that uses viewModelScope.
The text was updated successfully, but these errors were encountered:
Discussed in #118
Originally posted by mhernand40 September 18, 2022
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
.The text was updated successfully, but these errors were encountered: