如何使用长者preSSO在特定位置适配器测试项目 [英] How to use Espresso to test item in adapter at a specific position

查看:126
本文介绍了如何使用长者preSSO在特定位置适配器测试项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用长者preSSO(2.0),以确保在给定位置在文本列表中的适配器项是正确的,和我的生活中,我想不通的正确方法打电话。

I'm trying to use Espresso (2.0) to verify that the text in in a list adapter item at a given position is correct, and for the life of my I can't figure out the correct methods to call.

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

My Adapter type (IconRowAdapter) contains a list of IconRow objects. Each IconRow has a getText() method that returns the text of that item.

下面的非长者preSSO工作测试code,它验证在适配器在位置0 IconRow对象具有预期的文本(艺术家)。

Here's the non-Espresso working test code that verifies that the IconRow object at position 0 in the adapter has the expected text ("Artists").

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

    assertEquals(adapter.getItem(0).getText(), "Artists");
}

这工作。

我试过以下长者preSSO code什锦变化,以做同样的事情,

I've tried assorted variations on the following Espresso code to do the same thing,

onData(is(instanceOf(IconRowAdapter.class)))
        .atPosition(0)
        .check(matches(withItemContent("Artists")));

其中, 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);
        }
    };
}

什么我期望做的是:

What I would expect that to do is:


  • 获取来自这IconRowAdapter(其中有在活动只有一个)...
  • 的实例适配器的数据
  • ...找到位置0的适配器的条目...

  • ...使用withItemContent()来检查在该位置的项目文本艺术家匹配

  • Get the data from the adapter that's an instance of IconRowAdapter (of which there's only one in the activity)...
  • ... find the entry at position 0 in the adapter...
  • ... use withItemContent() to check that the text in the item at that position matches "Artists"

当我跑,我得到了以下错误:

When I run that I get the following error:

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]>

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

There are 12 items in IconRowAdapter, so I have some confidence that it's looking in the correct adapter.

所有的例子code和文件,我已经能够找到假定你试图找到在适配器为了一个条目来点击它(并认为这将导致一些价值另一种观点变化)。我找不到任何谈论如何如何检查在适配器给定项的值。

All the example code and documentation that I've been able to find assumes that you're trying to find an entry in the adapter in order to click on it (and that this will result in the value in some other view changing). I can't find anything that talks about how how to check the value of a given item in an adapter.

任何见解感激地接受。

编辑补充:

这是什么工作如下:

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

如果我的理解,适当的的测试R.id.text1浏览的价值,而不是在适配器中的价值。我想这是有道理的,一个UI测试,但我在寻找如何(如果?)我可以使用长者preSSO来测试适配器一个项目的内容仍然有兴趣。

If I understand that properly that's testing the value of the R.id.text1 view, rather than the value in the adapter. I guess that makes sense for a UI test, but I'm still interested in finding out how (if?) I can use Espresso to test the contents of an item in the adapter.

推荐答案

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

The matcher passed as argument to onData() must match the value as returned by Adapter.getItem(). So the first version doesn't match, because of the wrong type being used. It should be:

onData(is(instanceOf(IconRowAdapter.IconRow.class)))

也可以什么缺陷是在不同类型的CharSequences使用equalTo。串是一个的CharSequence,但如果IconRow.getText()返回的CharSequence代替串,那么这也可以是Spannable,编辑等在这种情况下equalTo将不匹配。所以,如果IconRow.getText()返回任何东西,但字符串,请务必将其转换为字符串比较之前。

What also can be a pitfall is using equalTo on different kinds of CharSequences. String is a CharSequence, but if IconRow.getText() returns CharSequence instead of String, then this can also be Spannable, Editable, etc in which case equalTo wouldn't match. So if IconRow.getText() return anything but String, make sure to convert it into a String before comparison.

这篇关于如何使用长者preSSO在特定位置适配器测试项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆