当检查textView文本是否以期望的字符串结尾时,为什么android espresso测试失败

问题描述 投票:0回答:2

我有一个android测试程序,在应用android:ellipsize =“ end”时检查外部短信是否被截断并以三个点结尾。我不知道为什么即使活动中显示的文本格式正确,测试也会失败。

enter image description here

@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("..."))));
}

screenshot of ErrorActivity

我使用从]导入的功能>

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布局中的errorMessageTextView声明

<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=""/>

我有一个android测试程序,在应用android:ellipsize =“ end”时检查外部短信是否被截断并以三个点结尾。我不知道为什么测试失败,尽管在...

android textview android-espresso matcher hamcrest
2个回答
0
投票

[可能是因为您要检查字符串"..."而不是字符串"…"(后者是一个字符而不是三个点字符)。


0
投票

您无法通过检查TextView是否以“ ...”结尾来测试android:ellipsize

这是因为即使您看到它以“ ...”结尾,但实际上并没有以“ ...”结尾。

© www.soinside.com 2019 - 2024. All rights reserved.