我有password.setError(getResources().getString(R.string.incorrect_data));如果我设置了无效的密码-在textView上显示“无效数据!”文本,
我需要通过Espresso对其进行测试,我写道:

onView(withText(R.string.incorrect_data)).check(matches(isDisplayed()));

但这是错误的,我有:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131493034>[incorrect_data] value: Invalid data!
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

如果我写:onView(withText("Invalid data!")).check(matches(isDisplayed()));
我有:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Invalid data!"
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

我使用浓缩咖啡2:
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.Espresso.onView;

请帮帮我。

最佳答案

帮助过我:

onView(withId(R.id.password)).check(matches(withError(
                getActivity().getString(R.string.incorrect_data))));

private static Matcher<View> withError(final String expected) {
        return new TypeSafeMatcher<View>() {

            @Override
            public boolean matchesSafely(View view) {
                if (!(view instanceof EditText)) {
                    return false;
                }
                EditText editText = (EditText) view;
                return editText.getError().toString().equals(expected);
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }

10-08 03:38