Express onData 测试

Espresso onData test

我正在为具有长动态条目列表的导航抽屉编写 Espresso 测试。我想通过文本而不是位置编号来匹配 navDrawer 菜单项。查看 Google 的 DataAdapterSample,我希望我可以使用它来匹配:

@Test
public void myTest() {
    openDrawer();
    onRow("Sign In").check(matches(isCompletelyDisplayed()));
}

private static DataInteraction onRow(String str) {
       return onData(hasEntry(equalTo("module_name"), is(str)));
}

我找不到匹配项。但是在日志中我可以看到我在找什么。我得到

No data found matching: map containing ["module_name"->is "Sign In"]
contained values: <[

Data: Row 0: {_id:"0", module_name:"Applications", module_secure:"false", headerCollapsible:1, } (class: android.database.MatrixCursor) token: 0, 
Data: Row 1: {_id:"1", module_name:"Sign In", module_lock:"false", module_right_text:null, } (class: android.database.MatrixCursor) token: 1, 
...

我认为 hasEntry() 仅适用于地图,在我看来您的导航抽屉中的项目不是地图而是 MatrixCursors。

只需将示例中的 Person class 替换为 MatrixCursor class。

例如这样的事情:

private static DataInteraction onRow(final String str) {
   return onData(new BoundedMatcher<Object, MatrixCursor>(MatrixCursor.class) {
      @Override
      public void describeTo(Description description) {
         description.appendText("Matching to MatrixCursor");
      }

      @Override
      protected boolean matchesSafely(MatrixCursor cursor) {
         return str.equals(cursor.getString(1));
      }
   });
}

这里我假设光标的第二列包含我们需要匹配的文本。我假设这是基于 "No data found matching.." 错误消息。