[FR]: Enhance flow testing methodology using backgroundScope
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the problem
Currently, our flow testing methodology in the NowInAndroid project requires manually creating and canceling job objects to collect flows during tests. This approach can be error-prone and doesn't align with the latest best practices recommended in the Android developer documentation. (example in viewModel test )
Describe the solution
I propose updating our flow testing methodology to utilize backgroundScope for collecting flows. This solution offers several benefits:
- Eliminates the need to manually create and cancel job objects.
- Provides automatic cancellation of the coroutine before the end of the test.
- Aligns with the latest best practices as documented by Android developers.
Implementation details:
• Replace current job creation and cancellation with backgroundScope. • Update relevant test cases to use this new methodology. • Ensure all tests pass with the new implementation.
References:
-
https://developer.android.com/kotlin/flow/test
-
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-scope/background-scope.html
i have already created a pull request implementing this change: https://github.com/android/nowinandroid/pull/1585
Additional context
This change will simplify our test code, reduce the risk of forgetting to cancel job objects, and improve our alignment with current Android development best practices. It will make our tests more robust and easier to maintain in the long run.
Code of Conduct
- [X] I agree to follow this project's Code of Conduct