Unit testing 如何访问视图并测试视图是否可见?

Unit testing 如何访问视图并测试视图是否可见?,unit-testing,junit,android-testing,robolectric,Unit Testing,Junit,Android Testing,Robolectric,我得到了碎片和if/else条件。基于此条件,textView可见性可见/消失。 如何在robolectric中进行模拟测试或访问该文本视图并检查可见性?使用robolectric作为在模拟器中启动应用程序的替代方法,您需要使用espresso来测试视图处于何种状态 @RunWith(RobolectricTestRunner::class) class MyUnitTest { @Before fun setup() { // Launch activity

我得到了碎片和if/else条件。基于此条件,textView可见性可见/消失。
如何在robolectric中进行模拟测试或访问该文本视图并检查可见性?

使用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)))
    } 
}