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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)