StateFlowwithSharingStarted.WhileSubscribeddoesn't emit until there's an active subscriber โ tests needbackgroundScope.launch { vm.flow.collect {} }or the flow never starts- In-memory fakes (a
MutableMap+MutableStateFlow) beat MockK for ViewModel tests: you test real state transitions, not just that a method was called StandardTestDispatcher+advanceUntilIdle()gives you deterministic control over coroutines โ nodelay(), no flaky timing- Writing the fakes also exposed bugs โ the
TrashViewModelwas creating a newMutableStateFlow()on everygetDeletedNotes()call instead of returning the same flow, so upstream emissions never reached the ViewModel - 38 passing JVM tests, no device required, runs in ~3 seconds on CI
๐ Today I Learned
Testing Android MVVM Without Mocks
ยท1 min readยท#today-i-learned#android#kotlin#testing#mvvm