代码之家  ›  专栏  ›  技术社区  ›  Rasoul Miri

通过单击“查看”来检查是否已使用Espresso启动新活动

  •  0
  • Rasoul Miri  · 技术社区  · 6 年前

     startActivityForResult(Intent(this, A::class.java)
    

    我需要签入一个esspresso测试时点击按钮,是否启动活动A?

    onView(withId(R.id.button))
           .check(matches(isDisplayed()))
           .check(matches(isEnabled()))
           .perform(click())
    
            // check is this A Activity start or not?
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Saeed Masoumi    6 年前

    你可以用 espresso-intents 包裹。

    首先,尝试将最新版本添加到 build.gradle :

       androidTestImplementation "androidx.test.espresso:espresso-intents:3.1.1"
    

    然后,使用 IntentsTestRule

        @get:Rule
        val intentRule = IntentsTestRule(MainActivity::class.java)
    
        @Test
        fun verify_FakeActivity_is_started() {
            onView(withId(R.id.button))
                .check(matches(isDisplayed()))
                .check(matches(isEnabled()))
                .perform(click())
    
            intended(hasComponent(FakeActivity::class.java.name))
        }