【发布时间】:2016-10-24 13:48:06
【问题描述】:
我目前正在尝试使用 Espresso 在 android 上测试 ListView,并使用自定义 ArrayAdapter。
这是测试:
final AssociationsListActivity listActivity = getActivity();
final Association association1 = new Association(ASSOCIATION_NAMES[0], ASSOCIATION_DESCRIPTIONS[0]);
final Association association2 = new Association(ASSOCIATION_NAMES[1], ASSOCIATION_DESCRIPTIONS[1]);
final Association association3 = new Association(ASSOCIATION_NAMES[2], ASSOCIATION_DESCRIPTIONS[2]);
listActivity.addAssociation(association1);
listActivity.addAssociation(association2);
listActivity.addAssociation(association3);
assertTrue(listActivity.getAssociationsList().size() == 3);
onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));
onData(anything())
.inAdapterView(withId(R.id.associationsListView))
.atPosition(0)
.perform(click());
assertCurrentActivityIsInstanceOf(AssociationsListActivityTest.this
, AssociationsPageActivity.class);
listActivity.addAssociation(association); 方法只执行associationArrayAdapter.add(association);。
ArrayAdapter用于将元素添加到ID为R.id.associationsListView的ListView(默认可见)
当我运行这个测试时,我收到以下错误:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"
这是由以下断言引起的,onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));
同样在评论这个断言时,下一个会导致以下异常:
android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: ch.epfl.sweng.project:id/associationsListView'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"
我经历了很多覆盖这些异常的堆栈溢出线程,我用其他等价物替换了这些测试,但这些错误仍然出现我不知道该怎么办!感谢您的帮助!
【问题讨论】:
标签: java android unit-testing testing android-espresso