断言ImageView已加载特定的可绘制资源ID [英] Assert ImageView was loaded with specific drawable resource ID

查看:60
本文介绍了断言ImageView已加载特定的可绘制资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 Robolectric 单元测试,我需要断言ImageView具有setImageResource(int)以特定的资源ID对其进行了调用.我正在使用 fest-android 进行断言,但似乎没有包含该断言.

I'm writing a Robolectric unit test and I need to make an assertion that an ImageView had setImageResource(int) called on it with a certain resource ID. I'm using fest-android for assertions but it doesn't appear to contain this assertion.

我还尝试获取 ShadowImageView 来自Robolectric的ImageView,因为我知道它曾经使您能够访问它,但现在不见了.

I also tried to get the ShadowImageView from Robolectric for the ImageView because I know it used to give you access to this, but it's now gone.

最后,我尝试在代码中调用setImageDrawable而不是setImageResource,然后在测试中断言如下:

Lastly, I tried to call setImageDrawable in my code instead of setImageResource, then in my test assert like this:

assertThat(imageView).hasDrawable(resources.getDrawable(R.drawable.some_drawable));

但是这也会失败,即使失败消息清楚地表明它与加载的Drawable相同.

but this also fails, even though the failure message clearly shows it's the same Drawable being loaded.

推荐答案

用于背景

ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getBackground()).getCreatedFromResId());

对于Drawable

ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getDrawable()).getCreatedFromResId());

这篇关于断言ImageView已加载特定的可绘制资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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