【发布时间】:2020-05-21 14:24:56
【问题描述】:
我编写了用于测试片段的代码。编写了测试方法 testChangeSystemsMeasuresToMetric、testChangeSystemsMeasuresToImperial、checkFragmentOpen,但是如果一起运行它们,testChangeSystemsMeasuresToImperial 会失败,并出现 androidx.test.espresso.AmbiguousViewMatcherException: 'is assignable class class error.Problem views are marking with '**** MATCHES **** ' 以下。 同时,每个 tete 都成功完成,如果同时运行 testChangeSystemsMeasuresToImperial 和 testChangeSystemsMeasuresToMetric,那么所有测试都会成功运行。可能是什么问题?
class ProfileFragmentTest {
@Rule
@JvmField
val activityRule = ActivityTestRule(MainActivity::class.java)
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)
private val testProfile = Profile(SystemsMeasures.Metric, ObservableBoolean(true),
ObservableInt(0), ObservableDouble(0.0))
@Before
fun setUp() {
CurrentProfile.currentProfile = testProfile
onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT)))
.perform(DrawerActions.open());
onView(withId(R.id.nav_view))
.perform(NavigationViewActions.navigateTo(R.id.nav_profile))
}
@Test
fun checkFragmentOpen() {
onView(withId(R.id.profile_fragment)).check(matches(isDisplayed()));
}
@Test
fun testChangeSystemsMeasuresToImperial() {
val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.poundsMassUnit)
clickChangeSystemsMeasures(itemText)
assert(testProfile.systemMeasures == SystemsMeasures.Imperial)
}
@Test
fun testChangeSystemsMeasuresToMetric() {
testProfile.systemMeasures = SystemsMeasures.Imperial
val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.kilogramsMassUnit)
clickChangeSystemsMeasures(itemText)
assert(testProfile.systemMeasures == SystemsMeasures.Metric)
}
private fun clickChangeSystemsMeasures(itemText: String) {
val spinnerId: Int = R.id.spinner
onView(withId(spinnerId)).perform(click())
onData(allOf(`is`(instanceOf(String::class.java)), `is`(itemText))).perform(click())
}
}
【问题讨论】:
标签: android kotlin automated-tests android-espresso