'Android Espresso Kotlin Flow not collecting data
I had written an instrumentation test (Espresso), which opens the fragment and tests if the recycler view is visible or not.
In my view model, I had a call to the local database which returns the Flow<List> and only collectLatest{} and update the live data to display on the screen.
I had put some logs to display the ongoing data.
viewModelScope.launch {
Log.d("testing", "inside scope")
dao.getUsers().filter { it.isNotEmpty() }.collectLatest {
Log.d("testing", it.toString())
processData(it)
}
}
I had setup all the required idling resources for testing and as I am using the Hilt library for dependency injection, I had setup their launch fragment container too.
@Before
fun init() {
hiltRule.inject()
IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
IdlingRegistry.getInstance().register(idlingResourcesForDataBinding)
populateDatabase()
}
@After
fun tearDown() {
IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
IdlingRegistry.getInstance().unregister(idlingResourcesForDataBinding)
appDatabase.close()
}
@Test
fun test_list_isDisplayed() {
launchFragmentInHiltContainer<MyFragment> {
idlingResourcesForDataBinding.monitorFragment<MyFragment>(this)
}
onView(withId(R.id.recyclerview))
.check(matches(isDisplayed()))
Log output while testing
D/testing: inside scope
I had confirmed if the populateDatabase was successful or not by simply using the dao object in the test and calling the get data method which returns the required data successfully.
My observation so far
I had tried with collect{} and collectLatest{} both flow operators and first it emits [] empty list which means the db is empty but. once the insertion successful, any of the collect operator does not receiving the datat.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
