我有一个 android 测试,检查应用 android:ellipsize="end" 时外部文本消息是否被截断并以三个点结尾。我不知道为什么测试失败,尽管活动中呈现的文本格式正确。
@Test
fun when_errorMessage_is_very_long_then_text_of_errorMessageTextView_ends_with_dots() {
//given
val errorMessage = """
Very long error, Very long error, Very long error, Very long error, Very long error,
Very long error, Very long error, Very long error, Very long error, Very long error,
Very long error, Very long error, Very long error, Very long error, Very long error,
Very long error, Very long error, Very long error, Very long error, Very long
error,"""
//when
presentErrorActivityWith(errorMessage)
//then
onView(withId(R.id.errorMessageTextView)).check(matches(withText(endsWith("..."))));
}
我使用从导入的功能
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
ErrorActivity 布局中的错误消息 TextView 声明
<TextView
android:id="@+id/errorMessageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:layout_marginTop="8dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
app:layout_constraintTop_toBottomOf="@+id/errorMessageTitleTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text=""/>