hatepoint / phrases-hyperskill-tests

0 stars 1 forks source link

RecyclerView.assertListItems #2

Closed hatepoint closed 1 year ago

hatepoint commented 1 year ago

It's not quite clear for me how this method can be used. What is the second argument (assertItems: (itemViewSupplier...) is needed for? I made a workaround for checking the number of items in the tests avoiding this function but I would like to know how to do it correctly.

hatepoint commented 1 year ago

@RedJocker

RedJocker commented 1 year ago

this is from the examples repository (https://github.com/RedJocker/hyperskillRoboletricExample/blob/main/Project%20Name/SqliteStorageExample/src/test/java/org/hyperskill/projectname/Stage1UnitTest.kt#L114-L127)

            rvList.assertListItems(expectedList) { itemViewSupplier, index, item ->
                val itemView = itemViewSupplier()
                val listItemTvTitle = itemView.findViewByString<TextView>("listItemTvTitle")
                val listItemTvDescription = itemView.findViewByString<TextView>("listItemTvDescription")

                val expectedTitle = item.first
                val expectedDescription = item.second

                val actualTitle = listItemTvTitle.text.toString()
                val actualDescription = listItemTvDescription.text.toString()

                assertEquals("wrong title", expectedTitle, actualTitle)
                assertEquals("wrong description", expectedDescription, actualDescription)
            }

I used to pass just itemView, but I had problems with that whenever there where state changes requiring solution to call notifyItemChanged. When that happens the recyclerView may change the View instance for that list item so tests then need to refresh the view reference by calling the supplier again.

assertListItems is a extension function, so you call it on a recyclerView, in this case rvList is a recyclerView. The first argument is a list of items that you expect to be backing the views of the recyclerView, each item is going to be passed as argument to assertItems so that you can compare the itemView against that item and make assertions. The index is also passed as argument just in case you need it.

RedJocker commented 1 year ago

if you just want to assert if the size is the expected size you can pass an empty lambda as assertItems

rvList.assertListItems(expectedList) {_, _, _ -> /* implicitSizeAssertion */ }
hatepoint commented 1 year ago

I think I might use more than just size checking, so I'll let you know how it went

RedJocker commented 1 year ago

I think I can close this, if you need further clarifications for this method you can reopen this issue.

ps: no I can't, you opened the issue and I'm not owner of the repository so you are the one that has to close it