其他分享
首页 > 其他分享> > android – Espresso测试,ImageView包含一个drawable

android – Espresso测试,ImageView包含一个drawable

作者:互联网

我从his medium post开始实现Daniele Bottilo的Drawable匹配器.

现在我想用它来测试我的图像视图是不是空的.我试过这个:

onView(withId(R.id.image)) 
        .check( matches( not(noDrawable()) ) );

它不起作用,IDE警告我

not(…guava.base.Predicate) in Predicates cannot be applied to (org.hamcrest.Matcher)

我是Espresso的新手,并没有成功地成功获得Google的答案.在我应该使用的另一个包装中是否有“不”,或者我在这里做错了什么?

解决方法:

我已经在Medium上回答了你,但我也会在这里发表回复;在EspressoTestsMatchers中,我会添加:

public static Matcher<View> hasDrawable() {
    return new DrawableMatcher(DrawableMatcher.ANY);
}

在DrawableMatcher中,您可以执行以下操作:

static final int EMPTY = -1;
static final int ANY = -2;

@Override
protected boolean matchesSafely(View target) {
    ...
    ImageView imageView = (ImageView) target;
    if (expectedId == EMPTY){
        return imageView.getDrawable() == null;
    }
    if (expectedId == ANY){
        return imageView.getDrawable() != null;
    }
    ...
}

实际上我想我应该根据你的要求更新我的帖子! hasDrawable()匹配器可能很有用:)

标签:android,android-espresso,hamcrest
来源: https://codeday.me/bug/20190727/1554290.html