-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot test SharedFlow #33
Comments
Can you turn this into an executable sample that I can run and debug locally? I don't use MutableSharedFlow, view models, runBlockingTest, or JUnit 5... so yeah it's not entirely clear what's to blame. |
Can you share a full stacktrace? @rakesh-sh2020 I have created a simple reproduction of this bug. My above hypothesis I think is wrong. Here's my test. @Test fun foo() = suspendTest {
val flow1 = MutableSharedFlow<Int>(replay = 0)
val flow2 = flow1.asSharedFlow()
flow1.emit(1)
flow2.test {
assertEquals(1, expectItem())
cancelAndConsumeRemainingEvents()
}
} with the corresponding stacktrace:
Notice how it's failing at My suspicion is that your @Test fun foo() = suspendTest {
val flow1 = MutableSharedFlow<Int>(replay = 0)
val flow2 = flow1.asSharedFlow()
launch {
flow2.test {
assertEquals(1, expectItem())
cancelAndConsumeRemainingEvents()
}
}
flow1.emit(1)
} |
@kevincianfarini You can try with:
|
I know the problem here The ViewModel function will be executed and the flow will be on the right track to be tested with Turbine 😉
|
Your problem could be with |
@kevincianfarini I can confirm that launching sharedFlow tests in a separate coroutine (Using |
@Kshitij09-ag it sets up a flow collector before a value is emitted to it. Once that collector is active and suspended, it will resume when a new emission from the flow is available. From there, the test assertion can be run. This entire issue happens because The solution @dinorahto provides does the exact same thing my solution does. It starts collecting the flow (with This issue is not an issue with Turbine, but instead a misunderstanding of the sequence of events that should happen with hot flows. It's possible the documentation should be updated to include this use case. |
@JakeWharton It's likely you can close this issue. If you're up for it, I can write some documentation for how turbine should be used with hot flows. |
Yeah I left #19 open to track adding documentation and I think this falls under that category. |
Hot flows are unique in that they drop emissions when no consumers are active. We should document this functionality as it has been surprising to many users so far. Closes cashapp#19. References cashapp#33.
Hot flows are unique in that they drop emissions when no consumers are active. We should document this functionality as it has been surprising to many users so far. Closes cashapp#19. References cashapp#33.
Hot flows are unique in that they drop emissions when no consumers are active. We should document this functionality as it has been surprising to many users so far. Closes cashapp#19. References cashapp#33.
I have an android viewmdeol class with the following property
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.
and I get
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.
The text was updated successfully, but these errors were encountered: