nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

[FR]: Enhance flow testing methodology using backgroundScope

Open saeedishayan76 opened this issue 1 year ago • 0 comments

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:

  1. Eliminates the need to manually create and cancel job objects.
  2. Provides automatic cancellation of the coroutine before the end of the test.
  3. 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:

  1. https://developer.android.com/kotlin/flow/test

  2. 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

saeedishayan76 avatar Sep 04 '24 21:09 saeedishayan76