-
Notifications
You must be signed in to change notification settings - Fork 106
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
When use liveData and change to flow by .asFlow() test won't trigger events #117
Comments
I have never used LiveData. Can you provide a self-contained failing test case? |
So we have some situation that live data is converted to flow in our useCases. In bellow test cases, test1 will fails but second will pass.
|
Hey @JakeWharton I feel issue is some-how about coroutine scope here:
And we should support this |
I see no issue with Turbine. Your testing approach was missing some key components. Both tests succeed here : class TurbinFlowTest {
// Needed to Unit Test LiveData
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
// Some stuff from coroutine testing 1.6, check https://www.youtube.com/watch?v=tCliepyQfHQ for example for more information
private val testCoroutineDispatcher = StandardTestDispatcher()
private val testCoroutineScope = TestScope(testCoroutineDispatcher)
private val flow1 = liveData {
for (i in 1..10) {
delay(100)
emit(i)
}
}.asFlow()
private val flow2 = flow {
for (i in 1..10) {
delay(100)
emit(i)
}
}
@Before
fun setUp() {
Dispatchers.setMain(testCoroutineDispatcher)
}
@After
fun teardown() {
Dispatchers.resetMain()
}
@Test
fun `Should catch events when use flow that converted from live data with asFlow()`() = testCoroutineScope.runTest {
flow1.test {
val item = awaitItem()
Assert.assertNotNull(item)
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun `Should catch events when use flow direct constructor`() = testCoroutineScope.runTest {
flow2.test {
val item = awaitItem()
Assert.assertNotNull(item)
cancelAndIgnoreRemainingEvents()
}
}
} I recommend you take a look to this wonderful guide too : https://developer.android.com/kotlin/coroutines/coroutines-best-practices |
Thanks for response, for minimizing the question I did not pass dispatchers and as long as no dispatcher change I think
Could you check if you see same issue? |
Hi
I have an issue when convert live data to flow like:
Then I got this error
After waiting for 60000 ms, the test coroutine is not completing, there were active child jobs: [ScopeCoroutine{Active}@35c3ef4a]
Previously I faced this issue and find out that in some case should
awaitComplete()
but it some case that will also not works.The text was updated successfully, but these errors were encountered: