android - 尝试匹配微调器和文本字段的文本内容,但 'withText' 和 'withSpinnerText' 正在返回有关组件的数据

标签 android kotlin android-espresso

我正在学习 Pluralsight 教程“使用 Kotlin 的 Android 应用程序:工具和测试”并遇到了以下问题:

在仪器化测试期间,我们检查微调器和两个文本字段中的文本,在视频中它正常运行,但我遇到了问题。这是我的测试代码:

import android.provider.ContactsContract
import android.support.test.runner.AndroidJUnit4
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import android.support.test.espresso.Espresso.*
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.action.ViewActions.*
import android.support.test.rule.ActivityTestRule
import org.hamcrest.Matchers.*
import org.junit.Rule
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.withSpinnerText
import android.support.test.espresso.matcher.ViewMatchers.withText

@RunWith(AndroidJUnit4::class)
class NextThroughNotesTest {
    @Rule @JvmField
    val noteListActivity = ActivityTestRule(NoteListActivity::class.java)

    @Test
    fun nextThroughNotes() {
        onData(allOf(instanceOf(NoteInfo::class.java), equalTo(DataManager.notes[0]))).perform(click())

        for (index in 0..DataManager.notes.lastIndex) {
            val note = DataManager.notes[index]

            onView(withId(R.id.spinnerCourses)).check(
                matches(withSpinnerText(note.course?.title)))
            onView(withId(R.id.textNoteTitle)).check(
                matches(withText(note.title)))
            onView(withId(R.id.textNoteText)).check(
                matches(withText(note.text)))

            if (index != DataManager.notes.lastIndex) {
                onView(allOf(withId(R.id.action_next), isEnabled())).perform()
            }
        }

        onView(withId(R.id.action_next)).check(matches(isEnabled()))
    }
}

查看错误消息,我得到以下信息:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Android Programming with Intents"' doesn't match the selected view.
Expected: with text: is "Android Programming with Intents"
Got: "AppCompatSpinner{id=2131230878, res-name=spinnerCourses, visibility=VISIBLE, width=912, height=63, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@1f42cb1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=84.0, y=42.0, child-count=1}"

如果我注释掉关于微调器的行,错误消息是相似的。我尝试按照我发现的以前的 StackOverflow 解决方案添加“containsString”,但显然这不起作用。我做错了什么?

最佳答案

要解决此问题,您需要确保以下库具有 androidTestImplementation 依赖项:

androidx.test:core
androidx.test:rules
androidx.test.ext:junit
androidx.test:runner
androidx.test.espresso:espresso-core

作为引用,以下是我最近创建的一个依赖 AndroidX 而不是 Android 支持库的项目的依赖部分:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

归功于 Jim Wilson, the author of the same course on Pluralsight .

关于android - 尝试匹配微调器和文本字段的文本内容,但 'withText' 和 'withSpinnerText' 正在返回有关组件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55701110/

相关文章:

java - 对代码的几个部分进行相同的异常处理。安卓系统

android - 如何获得自定义工具栏上菜单项的涟漪效果?

android - 如何在 android paging 3 库中标记数据集的结尾?

android - lateinit 属性尚未初始化我找不到初始化它的方法

android - Espresso : How to use R. androidTest文件夹的字符串资源

android - 使用 Espresso 和 Spoon 运行特定测试

android - Gradle 在 UI 中标记错误,但构建得很好

Android Gradle 4 升级问题(aapt、windowEnterAnimationt)——看起来像 "aapt2 issue"

android - Kotlin 构造函数属性 val 不能在类中使用

Android Studio 内存使用 Ubuntu 16