I got the Dogglers app to look correct, but when running the tests the grid_list_content_at_first_position failed and returned following error.
androidx.test.espresso.NoMatchingViewException: No views in
hierarchy found matching: an instance of android.widget.TextView
and view.getText() with or without transformation to match: is "7"
I looked into what checkFirstPosition()
is doing and it only appears to check that one item in the list has name="Tzeitel", age="7", hobbies="sunbathing",and R.drawable.tzeitel
. I think in my app age="Age: 7" not "7", which is causing the failure. I confirmed this by adding a println statement to the onBindViewHolder()
function below:
override fun onBindViewHolder(holder: DogCardViewHolder, position: Int) {
// TODO: Get the data at the current position
val item = dogList[position]
// TODO: Set the image resource for the current dog
holder.imageView?.setImageResource(item.imageResourceId)
// TODO: Set the text for the current dog's name
holder.nameView?.text = item.name
// TODO: Set the text for the current dog's age
val resources = context?.resources
holder.ageView?.text = resources?.getString(R.string.dog_age, item.age)
println("AGEVIEW=${holder.ageView?.text}")
// TODO: Set the text for the current dog's hobbies by passing the hobbies to the
// R.string.dog_hobbies string constant.
// Passing an argument to the string resource looks like:
// resources?.getString(R.string.dog_hobbies, dog.hobbies)
holder.hobbiesView?.text = resources?.getString(R.string.dog_hobbies, item.hobbies)
}
the string resource for dog_age is:
"<string name="dog_age">Age: %1$s</string>"
My thought is that I am supposed to somehow define the formatted string within the textView and only pass "7" in, but I’m not sure how. I looked here but didn’t see anything about using it in a textView.
2
Answers
check your onBindViewHolder function.
the textView will get the value which you passed.
for the solution, either we change the test code to
or we change onBindViewHolder like:
You need the
containsString
matcher for the age and hobbies string