使用doesNotExist的Android Espresso onData

使用doesNotExist的Android Espresso onData,第1张

概述我试图验证ListView不包含特定项目.这是我正在使用的代码: onData(allOf(is(instanceOf(Contact.class)), is(withContactItemName(is("TestName"))))) .check(doesNotExist()); 当名称存在时,由于检查(didNotExist()),我正确地得到错误.当名称不存在时,我收到以下错误 我试图验证ListVIEw不包含特定项目.这是我正在使用的代码:
onData(allOf(is(instanceOf(Contact.class)),is(withContactItemname(is("Testname")))))      .check(doesNotExist());

当名称存在时,由于检查(dIDNotExist()),我正确地得到错误.当名称不存在时,我收到以下错误,因为allOf(…)不匹配任何内容:

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())?

编辑:

通过使用try / catch并检查事件的getCause(),我有一个可怕的黑客来获得我想要的功能.我很想用一种好的技术取而代之.

解决方法 根据Espresso样本,您不能使用onData(…)来检查适配器中是否存在视图.检查一下 – link.阅读“断言数据项不在适配器中”部分.您必须与找到AdapterVIEw的onVIEw()一起使用匹配器.

基于以上链接的Espresso样品:

>匹配器:

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:

@SuppressWarnings("unchecked")public voID testDataItemnotinAdapter(){    onVIEw(withID(R.ID.List))        .check(matches(not(withAdaptedData(is(withContactItemname("Testname"))))));}

还有一个建议 – 避免写入是(withContactItemname(is(“Testname”))将以下代码添加到匹配器:

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

然后你会有更多可读和清晰的代码(withContactItemname(“Testname”))

总结

以上是内存溢出为你收集整理的使用doesNotExist的Android Espresso onData全部内容,希望文章能够帮你解决使用doesNotExist的Android Espresso onData所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存