Ответ 1
В дополнение к предыдущему ответу, я создаю полную версию с двумя типами проверок. Это может помочь вам понять Espresso и Custom Matchers.
Разница со стандартным примером Espresso LongList заключается в том, что я использую список пользовательских объектов для отображения в списке. Прокрутка к списку справа и проверка результата показана ниже.
Способ 1 - проверка на строку
В тесте script есть:
onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myCustomObjectShouldHaveString( "my_item: 60")))
.perform(click());
// testing the result ... as in the longlist example
onView(withId(R.id.selection_pos2)).check(matches(withText("my_item: 60")));
Соединитель:
public static Matcher<Object> myCustomObjectShouldHaveString( String expectedTest) {
return myCustomObjectShouldHaveString( equalTo( expectedTest));
}
private static Matcher<Object> myCustomObjectShouldHaveString(final Matcher<String> expectedObject) {
return new BoundedMatcher<Object, MyObjectWithItemAndSize>( MyObjectWithItemAndSize.class) {
@Override
public boolean matchesSafely(final MyObjectWithItemAndSize actualObject) {
// next line is important ... requiring a String having an "equals" method
if( expectedObject.matches( actualObject.item) ) {
return true;
} else {
return false;
}
}
@Override
public void describeTo(final Description description) {
// could be improved, of course
description.appendText("getnumber should return ");
}
};
}
Способ 2: проверка на (полный объект).
В тесте script вы найдете:
MyObjectWithItemAndSize myObject = new MyObjectWithItemAndSize( "my_item: 60", 11);
onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myObjectHasContent( myObject))).perform( click());
onView(withId( R.id.selection_pos2)).check(matches(withText("my_item: 60")));
Соединитель.
Самая важная строка (с которой я боролся) ниже // ****
public static Matcher<Object> myObjectHasContent( MyObjectWithItemAndSize expectedObject) {
return myObjectHasContent( equalTo( expectedObject));
}
//private method that does the work of matching
private static Matcher<Object> myObjectHasContent(final Matcher<MyObjectWithItemAndSize> expectedObject) {
return new BoundedMatcher<Object, MyObjectWithItemAndSize>(MyObjectWithItemAndSize.class) {
@Override
public boolean matchesSafely( final MyObjectWithItemAndSize actualObject) {
// ****** ... the 'matches'. See below.
// this requires the MyObjectWithItemAndSize to have an 'equals' method
if( expectedObject.matches( actualObject) ) {
return true;
} else {
return false;
}
}
@Override
public void describeTo(final Description description) {
description.appendText("getnumber should return ");
}
};
}
Что очень важно, так это то, что пользовательский объект имеет этот метод (и я думаю, переопределяю):
@Override
public boolean equals( Object mob2) {
return( (this.item.equals( ((MyObjectWithItemAndSize) mob2).item)));
// of course, could have also a check on this.size.
}
И это работает!!!! Pfff, потребовалось некоторое время, но победил. Спасибо также Yash F.