Robolectric RoboAttributeSet - 如何使用它

Geo*_*pas 9 android unit-testing robolectric

我有一个自定义的android视图,其中有我想测试的可设置属性.我正在尝试使用RoboAttributeSet将这些推送到我的视图的构造函数中,但是对于我的生活,我无法找到我需要用来使它们工作的正确语法.无论我尝试什么,在robolectric测试中运行时,视图都没有拾取我正在推送的属性.在设备或模拟器上运行应用程序很好.

有没有人可以使用这些的例子?有谁知道如何做到这一点?这是我的自定义视图的代码片段以及它如何使用可设置的属性.

TypedArray customProperties = aContext.getTheme().obtainStyledAttributes(aAttrs, R.styleable.LoginView, 0, 0);

try {
  userName.setHint(customProperties.getString(R.styleable.LoginView_username_hint));
} finally {
  customProperties.recycle();
}
Run Code Online (Sandbox Code Playgroud)

这是我的Robolectric/Spock单元测试的一个片段......

given:
AttributeSet attributes = new RoboAttributeSet(
        [new Attribute("com.acme:attr/username_hint", "myhint", "com.acme")], Robolectric.application.resources, LoginView)

when:
view = new LoginView(Robolectric.application, attributes)

then:
view.getUsername().getHint() == "myhint"
Run Code Online (Sandbox Code Playgroud)

感谢
乔治

Joh*_*ley 7

对于那些使用RoboElectric新版本3的人来说,从他们的github上查看这个样本,了解如何使用ResourceLoader.

https://github.com/robolectric/robolectric/blob/490e5fcb7165bec9ef2ddc1c937af790806bb13d/robolectric/src/test/java/org/robolectric/shadows/ViewStubTest.java#L59

让我找到一个像这样的例子!

编辑:

我只是想更新这个帖子,因为过去一年里很多事情都发生了变化.谷歌在某种程度上提供了围绕Android测试的官方文档.你可以找到它

这里:https://google.github.io/android-testing-support-library/

&这里:https://github.com/googlesamples/android-testing