Espresso ViewAssertions - 检查是否为空

Espresso ViewAssertions - Check If Not Null

我正在运行 Espresso 测试,我想测试 2 个项目:

  1. 点击 Fragment X 中的按钮
  2. TextView 包含 Activity Y
  3. 中的文本

代码:

@RunWith(AndroidJUnit4.class)
public class AsyncTest {

    private static final String TEST_TEXT = "TEST_TEXT";

    @Rule
    public ActivityTestRule<MainActivity> mActivity = new ActivityTestRule(MainActivity.class);

    @Test
    public void testIfResultIsEmpty() {

        Espresso.onView(ViewMatchers.withId(R.id.first_button))
          .perform(ViewActions.click());

        /* How do I check that text has populated? */
        Espresso.onView(ViewMatchers.withId(R.id.random_joke))
         .check();
 }

matches(isDisplayed()) 至少会检查视图是否正在显示 - 虽然人们几乎无法检查随机笑话的文本,但人们会假设一些随机文本......除非加载特定文本测试目的,然后才知道 - 可能是 compared/matched(正如最近在 RecyclerView 的回答中解释的那样,ActivityTestRule 可用于传递 ITEM_ID,这样人们就会有要比较视图的特定项目)。

onView(withId(R.id.random_joke)).check(matches(isDisplayed()));

而且不一定要使用 Espresso,但可以尝试 findViewById() 然后 assertNotNull()