Espresso - 验证 TextView 内容 Android

Espresso - Validate TextView contents Android

我正在尝试在 Android 上编写 Expresso 测试以验证 TextView 内容。 当我从下面的资源中阅读文本时,它起作用了

    @Test
    public void changeText_newActivity() {
        onView(withId(R.id.mainContent)).check(matches(withText("Hello World!")));
}

以上是使用espresso的测试 下面是 android activity 上有效的代码

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainContent"
    android:text="@string/hello_world"
    />

但是当我使用硬编码文本替换文本而不是引用资源时,测试失败并收到无法在视图层次结构中找到视图的错误

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainContent"
    android:text="Hello world!"
    />

下面是我运行测试时的错误

android.support.test.espresso.NoMatchingViewException:在层次结构中找不到匹配的视图:与文本:是 "Hello world!"

提前感谢您的帮助。

Code    "Hello world!"
               ^
Test    "Hello World!"

看出区别了吗? Ww 不一样。

Espresso 理所当然地找不到这样的视图,因为有 none 数学。

使用下面的代码

onView(withId(R.id.mainContent)).check(matches(withText("Hello world!")));

在某些情况下,eqaulIgnoreCase 也是更好的选择。