具有didNotExist的Android Espresso onData [英] Android Espresso onData with doesNotExist

查看:57
本文介绍了具有didNotExist的Android Espresso onData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证ListView不包含特定项目.这是我正在使用的代码:

I am trying to verify that a ListView does not contain a particular item. Here's the code I'm using:

onData(allOf(is(instanceOf(Contact.class)), is(withContactItemName(is("TestName")))))
      .check(doesNotExist());

当名称存在时,由于check(doesNotExist()),我正确地收到一个错误.如果名称不存在,则会出现以下错误,因为allOf(...)不匹配任何内容:

When the name exists, I correctly get an error because of check(doesNotExist()). When the name does not exist, I get the following error, because allOf(...) doesn't match anything:

Caused by: java.lang.RuntimeException: No data found matching: 
(is an instance of layer.sdk.contacts.Contact and is with contact item name:
is "TestName")

我如何获得onData(...).check(doesNotExist())之类的功能?

How can I get functionality like onData(...).check(doesNotExist())?

我有一个可怕的技巧,可以通过使用try/catch并检查事件的getCause()来获得所需的功能.我想用一种好的技术来代替它.

I have a terrible hack to get the functionality I'd like by using try/catch and inspecting the event's getCause(). I would love to replace this with a good technique.

推荐答案

根据Espresso示例,您不得使用onData(...)来检查适配器中是否不存在视图.检查一下-onView()一起使用.

According to Espresso samples you must not use onData(...) to check if view doesn't exists in adapter. Check this out - link. Read "Asserting that a data item is not in an adapter" part. You have to use a matcher together with onView() that finds the AdapterView.

基于上面链接中的Espresso示例:

Based on Espresso samples from link above:

  1. 匹配器:

  1. matcher:

private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("with class name: ");
            dataMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof AdapterView)) {
                return false;
            }

            @SuppressWarnings("rawtypes")
            Adapter adapter = ((AdapterView) view).getAdapter();
            for (int i = 0; i < adapter.getCount(); i++) {
                if (dataMatcher.matches(adapter.getItem(i))) {
                    return true;
                }
            }
            return false;
        }
    };
}

  • 然后是onView(...),其中R.id.list是适配器ListView的ID:

  • then onView(...), where R.id.list is the id of your adapter ListView:

    @SuppressWarnings("unchecked")
    public void testDataItemNotInAdapter(){
        onView(withId(R.id.list))
            .check(matches(not(withAdaptedData(is(withContactItemName("TestName"))))));
    }
    

  • 还有一个建议-为避免编写is(withContactItemName(is("TestName")),将以下代码添加到您的匹配器中:

    And one more suggestion - to avoid writing is(withContactItemName(is("TestName")) add below code to your matcher:

        public static Matcher<Object> withContactItemName(String itemText) {
            checkArgument( itemText != null );
            return withContactItemName(equalTo(itemText));
        }
    

    然后,您将获得更具可读性和更清晰的代码is(withContactItemName("TestName")

    then you'll have more readable and clear code is(withContactItemName("TestName")

    这篇关于具有didNotExist的Android Espresso onData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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