问题描述
我得到了片段并得到了 if/else 条件。基于此条件 textView 可见性 VISIBLE / GONE。 如何在 robolectric 测试模拟或访问该 textView 并检查可见性?
解决方法
Robolectric 用作在模拟器中启动应用程序的替代方法,您需要使用 espresso 来测试视图所处的状态。
@RunWith(RobolectricTestRunner::class)
class MyUnitTest {
@Before
fun setup() {
// Launch activity or fragment
}
@Test
fun 'view is visble'() {
// when viewmodel is visible
// then,Espresso
onView(withId(R.id.MyViewId))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
@Test
fun 'view is gone'() {
// then,Espresso
onView(withId(R.id.MyViewId))
.check(matches(withEffectiveVisibility(Visibility.GONE)))
}
}
https://android.github.io/android-test/downloads/espresso-cheat-sheet-2.1.0.pdf