2017-04-21 69 views
1

我想在Android上编写一个Expresso测试来验证TextView内容。 当我读从资源文本如下它的工作原理Espresso - 验证TextView内容Android

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

以上是用咖啡 下面的测试是在Android上的活动代码在那里工作

<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!”

提前赞赏您的帮助。

回答

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

看到区别? Ww是不一样的。

意式浓缩咖啡无法找到这样的观点,因为没有任何东西。

0

下面的代码

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

使用在某些情况下也eqaulIgnoreCase更好的选择。