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

时间:2019-04-16 05:27:16

标签: 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',但显然不起作用。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

要解决此问题,您需要确保以下库具有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