-
Notifications
You must be signed in to change notification settings - Fork 136
Android Basics: Dogglers app #37
Description
In which task and step of the codelab can this issue be found?
4. Test your app
Describe the problem
4 of the 19 tests faill
For 3 of the tests, they all fail for the same reason.
The test is not handling the string resources with variables.
This happens for age and hobbies
<string name="dog_age">Age: %1$s</string>
<string name="dog_hobbies">Hobbies: %1$s</string>
But in BaseTest.hasListItemContent() you have this:
onView(withText(age))
.check(matches(isDisplayed()))
onView(withText(hobbies))
.check(matches(isDisplayed()))
This won't work because the actual TextView will be a like "Age: 7" and you can calling withText("7")
So to fix you can just do this:
-
Add a private method so you don't duplicate your code (dry)
private fun getResourceStringWithVariable(id: Int, variable: String): String? {
val targetContext: Context = ApplicationProvider.getApplicationContext()
return targetContext.getResources().getString(id, variable)
} -
Fix your tests to be this:
onView(withText(getResourceStringWithVariable(R.string.dog_age, age))) .check(matches(isDisplayed())) onView(withText(getResourceStringWithVariable(R.string.dog_hobbies, hobbies))) .check(matches(isDisplayed()))
The fourth test HorizontalListTests.horizontal_scrolling() is failing, because at least on on my emulator (pixel 5 api 31) if you do a swipeLeft you actually go inbetween the third and fourth card so the nama showing is from the fourth card "Nox" not the third card "Frankie" So instead of
fun `horizontal_scrolling`() {
onView(withId(R.id.horizontal_recycler_view))
.perform(swipeLeft())
onView(withText("Nox")).check(matches(isDisplayed()))
}
it should be
fun `horizontal_scrolling`() {
onView(withId(R.id.horizontal_recycler_view))
.perform(swipeLeft())
onView(withText("Nox")).check(matches(isDisplayed()))
}
Steps to reproduce?
- Run the tests and you can see 4 failing ones.
Versions
Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 18, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 10.15.7
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 4
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.intellij.plugins.markdown (212.5457.16), Dart (212.5744), io.flutter (69.0.2)
Additional information
Include screenshots if they would be useful in clarifying the problem.
