通过在适配器中查找其对象来测试ListView与Espresso

时间:2018-04-12 07:11:11

标签: android android-espresso ui-testing

我有一个自定义匹配器,用于测试ListView中是否存在某些数据。 它找到listview适配器保存的所有对象:

private static Matcher<Object> checkStringInList(final Matcher<String> expected) {
    return new BoundedMatcher<Object, MyObject>(MyObject.class) {
        @Override
        public boolean matchesSafely(final MyObject actualObject) {
            return expected.matches(actualObject.getDisplayName());
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("row with string " + expected.toString() + " was not found!");
        }
    };
}

我正在使用它:

onData(allOf(instanceOf(MyObject.class), checkStringInList(equalTo("Teams")))).check(matches(isDisplayed()));

上述测试将通过。好吗!

但是当我传递一个在列表中不存在的字符串并且我真的想要检查它是否存在时,会发生奇怪的事情

onData(allOf(instanceOf(MyObject.class), checkStringInList(equalTo("TeamsSSS")))).check(doesNotExist());

奇怪的错误是:

    android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'.
    at android.support.test.espresso.PerformException$Builder.build(PerformException.java:84)
    at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
    at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
    at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
    at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
    at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
...
Caused by: java.lang.RuntimeException: No data found matching: (an instance of com.test.me.datamodel.MyObject and row with string "TeamsSSS" was not found!)

我已经知道字符串不存在了,我正在检查它,但是为什么它没有通过呢?

如果我的列表是ExpandableListView,这有关系吗?它也是android.widget.AdapterView的后代。

1 个答案:

答案 0 :(得分:0)

您需要另一个匹配程序来检查项目是否不在列表中。

可能,espresso作者为此提供了sample

以下是该链接的匹配器:

    onView(withId(R.id.list))
      .check(matches(not(withAdaptedData(withItemContent("item: 168")))));

和用法:

 'use strict';

function renderArtistFeatures() {
    var createFeatures = [{
        feature: 'free',
        Icon: FreeIcon,
        title: 'Main title',
        copy: 'Some sample text with a <strong>HTML tag</strong>'
    }].map(function (_ref, i) {
        var feature = _ref.feature,
            Icon = _ref.Icon,
            title = _ref.title,
            copy = _ref.copy;

        var klass = 'create-feature create-feature--' + feature + ' column small-24 medium-8';

        return React.createElement(
            'div',
            { key: i, className: klass },
            React.createElement(
                'div',
                { className: 'create-feature__inner' },
                React.createElement(
                    'div',
                    { className: 'create-feature__icon create-feature__icon--free' },
                    React.createElement(Icon, null)
                ),
                React.createElement(
                    'div',
                    { className: 'create-feature__content' },
                    React.createElement(
                        'h2',
                        { className: 'create-feature__title' },
                        title
                    ),
                    React.createElement(
                        'p',
                        null,
                        copy
                    )
                )
            )
        );
    });
}

renderArtistFeatures();