skip to Main Content

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


  1. check your onBindViewHolder function.

    holder.textViewNm.text = dog.name
    holder.textViewAge.text = resources.getString(R.string.dog_age, dog.age)
    holder.textViewHobbies.text = resources.getString(R.string.dog_hobbies, dog.hobbies)
    

    the textView will get the value which you passed.
    for the solution, either we change the test code to

    fun checkFirstPosition() {
    hasListItemContent("Tzeitel", "Age: 7", "Hobbies: sunbathing",
        R.drawable.tzeitel)}
    

    or we change onBindViewHolder like:

    holder.textViewAge.text = dog.age
    holder.textViewHobbies.text = dog.hobbies
    
    Login or Signup to reply.
  2. You need the containsString matcher for the age and hobbies string

        private fun hasListItemContent(name: String, age: String, hobbies: String, imageResource: Int) {
        onView(withText(name))
            .check(matches(isDisplayed()))
        onView(withText(containsString(age)))
            .check(matches(isDisplayed()))
        onView(withText(containsString(hobbies)))
            .check(matches(isDisplayed()))
        onView(withDrawable(imageResource))
            .check(matches(isDisplayed()))
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search