如何使用Espresso在特定位置测试适配器中的项目

Nik*_*Nik 5 android android-adapter android-espresso

我想用咖啡(2.0),以确保在给定的位置在文本列表中的适配器项是正确的,而对于我,我不能找出正确的方式生活打电话.

我的适配器类型(IconRowAdapter)包含一个IconRow对象列表.每个IconRow都有一个getText()返回该项目文本的方法.

这是非Espresso工作测试代码,用于验证适配器中位置0处的IconRow对象是否具有预期文本("Artists").

public void testHomeActivityMenu() {
    ListView list = (ListView) getActivity().findViewById(R.id.item_list);
    IconRowAdapter adapter = (IconRowAdapter) list.getAdapter();

    assertEquals(adapter.getItem(0).getText(), "Artists");
}
Run Code Online (Sandbox Code Playgroud)

这有效.

我尝试了以下Espresso代码的各种变体来做同样的事情,

onData(is(instanceOf(IconRowAdapter.class)))
        .atPosition(0)
        .check(matches(withItemContent("Artists")));
Run Code Online (Sandbox Code Playgroud)

这里withItemContent()看起来是这样的:

public static Matcher<Object> withItemContent(String expectedText) {
    checkNotNull(expectedText);
    return withItemContent(equalTo(expectedText));
}

@SuppressWarnings("rawtypes")
public static Matcher<Object> withItemContent(final Matcher<String> itemTextMatcher) {
    checkNotNull(itemTextMatcher);
    return new BoundedMatcher<Object, IconRow>(IconRow.class) {
        @Override
        public boolean matchesSafely(IconRow iconRow) {
            return itemTextMatcher.matches(iconRow.getText());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with item content: ");
            itemTextMatcher.describeTo(description);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我期望这样做是:

  • 从适配器获取数据,该适配器是IconRowAdapter的一个实例(活动中只有一个)...
  • ...找到适配器中位置0的条目...
  • ...使用withItemContent()检查该位置项目中的文本是否与"艺术家"匹配

当我运行时,我收到以下错误:

Caused by: java.lang.RuntimeException: No data found matching: is an instance of 
uk.org.ngo.squeezer.IconRowAdapter contained values: <[Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585980 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 0, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b15859e0 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 1, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585a00 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 2, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585a20 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 3, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585a40 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 4, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585a60 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 5, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585a80 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 6, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585aa0 (class:  
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 7, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585ac0 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 8, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585ae0 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 9, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585b00 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 10, Data: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow@b1585b20 (class: 
uk.org.ngo.squeezer.IconRowAdapter$IconRow) token: 11]>
Run Code Online (Sandbox Code Playgroud)

IconRowAdapter中有12个项目,所以我有信心它正在寻找正确的适配器.

所有的示例代码和文档,我已经能够找到假定你试图找到在适配器中的条目,以点击它(并认为这将导致一些其他考虑到改变的值).我找不到任何有关如何检查适配器中给定项的值的内容.

任何见解都感激不尽.

编辑添加:

工作如下:

onData(anything())
        .inAdapterView(withId(R.id.item_list))
        .atPosition(0)
        .check(matches(hasDescendant(
                allOf(withId(R.id.text1), withText(containsString("Artists"))))));
Run Code Online (Sandbox Code Playgroud)

如果我理解正确的是测试R.id.text1视图的值,而不是适配器中的值.我想这对于UI测试是有意义的,但我仍然有兴趣了解如何(如果?)我可以使用Espresso来测试适配器中项目的内容.

haf*_*fax 5

作为参数传递给的匹配器onData()必须匹配所返回的值Adapter.getItem()。因此,第一个版本不匹配,因为使用了错误的类型。它应该是:

onData(is(instanceOf(IconRowAdapter.IconRow.class)))
Run Code Online (Sandbox Code Playgroud)

在不同类型的CharSequences上使用equalTo也可能是一个陷阱。String是一个CharSequence,但是如果IconRow.getText()返回CharSequence而不是String,那么它也可以是Spannable,Editable等,在这种情况下equalTo将不匹配。因此,如果IconRow.getText()返回String以外的任何内容,请确保在比较之前将其转换为String。


den*_*nys 1

试试这个它一定有效:

//check that item is present in list at position 0
onData(withItemContent("Artists"))
    .inAdapterView(withId(R.id.list_view))
    .atPosition(0)
    .check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

如果您删除atPosition(0),那么您只需检查适配器视图中的存在情况。