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
(Apologies if this should be an issue for Turbine)
I have the following BaseViewModel class which is a lot like the class in the sample-viewmodel folder:
This works great inside an actual Android app, but testing it is proving to be a pain with the following failing test:
@Test
fun`selectedPet gets updated after selecting a pet from the list using viewmodel`() = runTest(timeout =500.milliseconds) {
val testDispatcher =StandardTestDispatcher(testScheduler)
val testScope =TestScope(testScheduler)
val viewModel =PetListViewModel(petDb, testDispatcher, testScope, RecompositionMode.Immediate)
viewModel.models.test {
println("marker 1")
assertEquals(null, awaitItem().selectedPet)
viewModel.take(PetListEvent.PetSelected(Pet(0, "Sparky")))
println("marker 2")
assertEquals(Pet(0, "Sparky"), awaitItem().selectedPet)
println("marker 3")
}
}
Which fails with this message:
Expected :Pet(id=0, name=Sparky)
Actual :null
Here's a test that succeeds, skipping the PetListViewModel entirely and using the composable function directly:
@Test
fun`selectedPet gets updated after selecting a pet from the list`() = runTest {
val testDispatcher =StandardTestDispatcher(testScheduler)
val testScope =TestScope(testScheduler)
val events =Channel<PetListEvent>()
testScope.launchMolecule(RecompositionMode.Immediate) {
PetListPresenter(events = events.receiveAsFlow(), petDb = petDb, testDispatcher)
}.test {
println("marker 1")
assertEquals(null, awaitItem().selectedPet)
events.send(PetListEvent.PetSelected(Pet(0, "Sparky")))
println("marker 2")
assertEquals(Pet(0, "Sparky"), awaitItem().selectedPet)
println("marker 3")
}
}
Am I using a wrong CoroutineScope here? I've tried using TestScope and CoroutineScope but no luck. I should note that the PetListViewModel works great inside an Android app with the following instantiation:
This doesn't look like a Turbine issue, no. I have some free advice, though:
Using a channel under tests for events is a good idea; be wary, though, of the possibility of the test subject collecting twice on the resulting flow. If this happens, only one collector will receive each event. Ifyou run into this, it can be fixed with shareIn.
Inside runTest, this will refer to a TestScope, which has a backgroundScope val; inject backgroundScope as your CoroutineScope.
For your dispatcher, inject CoroutineContext instead of CoroutineDispatcher; that way, you can inject EmptyCoroutineContext, which is a no-op under test (it will just use whatever dispatcher is already active).
You may need to add a distinctUntilChanged() before testing your molecule.
(Apologies if this should be an issue for Turbine)
I have the following
BaseViewModel
class which is a lot like the class in the sample-viewmodel folder:Note that
BaseViewModel
does not extend from the AACViewModel
.Here is an implementation of
BaseViewModel
+ composable presenter:This works great inside an actual Android app, but testing it is proving to be a pain with the following failing test:
Which fails with this message:
Here's a test that succeeds, skipping the
PetListViewModel
entirely and using the composable function directly:Am I using a wrong
CoroutineScope
here? I've tried usingTestScope
andCoroutineScope
but no luck. I should note that thePetListViewModel
works great inside an Android app with the following instantiation:The text was updated successfully, but these errors were encountered: