question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot test SharedFlow

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
dinorahtocommented, May 1, 2021

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

    @ExperimentalTime
    @Test
    fun `navigate`() = suspendTest {
        viewModel.navigationEvents.test {
            viewModel.handleIntent(TrainingViewModel.TrainingIntent.ShowQuestions)
            assertEquals(
                TrainingViewModel.TrainingNavigationEvent.NavigateToQuestions::class,
                expectItem()::class
            )
            cancelAndConsumeRemainingEvents()
        }
    }
1reaction
sdowardcommented, May 7, 2021

Your problem could be with runBlockingTest as it automatically advances time (as far as I remember)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found