Cannot test SharedFlow
See original GitHub issueI have an android viewmdeol class with the following property
private val _trainingNavigationEvents = MutableSharedFlow<NavigationEventTraining>(replay = 0)
val trainingNavigationEvents = _trainingNavigationEvents.asSharedFlow()
fun navigate(navigationEvent: NavigationEventTraining) {
viewModelScope.launch {
_trainingNavigationEvents.emit(navigationEvent)
}
}
I am using a SharedFlow as it solves the SingleLiveEvent problem.
The issue arises when I try and unit test the code. I can’t see how to use turbine (or supplied primitives) to get it to work.
@ExperimentalTime
@Test
fun `navigate`() = runBlockingTest {
viewModel.handleIntent(TrainingViewModel.TrainingIntent.ShowQuestions)
viewModel.navigationEvents.test {
assertEquals(
TrainingViewModel.TrainingNavigationEvent.NavigateToQuestions::class,
expectItem()::class
)
cancelAndConsumeRemainingEvents()
}
}
and I get
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms
I know that a SharedFlow never completes and that may be part of the reason but I have been unable to find any examples of how to do this instead.
I am using Junit 5 and am using a TestCoroutineDispatcher class extension.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How to unit test that a MutableSharedFlow<T>(replay=0) has ...
If you want to test with replay=1, you can try to emit before "observing/collecting", so before the job started.
Read more >Learning State & Shared Flows with Unit Tests
In this article, we will explore features of shared and state flows with unit testing. State FlowPermalink. Problem. The most common task in ......
Read more >Unit Testing MutableSharedFlow - jeffreysoboe - Medium
The crux of the problem is that MutableSharedFlow does not retain a value, and .collect() (which is a suspend function) does not ever...
Read more >Unit Test with Kotlin Flow - Level Up Coding - gitconnected
We decided to rewrite our tests and to play with Turbine — a small but powerful testing library for Kotlin Flow. It allows...
Read more >StateFlow and SharedFlow - Android Developers
For details, see the Testing Kotlin flows on Android page. StateFlow, Flow, and LiveData. StateFlow and LiveData have similarities. Both are ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I know the problem here For ViewModel you wanna do this:
The ViewModel function will be executed and the flow will be on the right track to be tested with Turbine 😉 Hope this help you
Your problem could be with
runBlockingTest
as it automatically advances time (as far as I remember)