Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试匹配微调器和文本字段的文本内容,但';带文本';和';使用spinnertext';而是返回有关组件的数据_Android_Kotlin_Android Espresso - Fatal编程技术网

Android 尝试匹配微调器和文本字段的文本内容,但';带文本';和';使用spinnertext';而是返回有关组件的数据

Android 尝试匹配微调器和文本字段的文本内容,但';带文本';和';使用spinnertext';而是返回有关组件的数据,android,kotlin,android-espresso,Android,Kotlin,Android Espresso,我正在关注Pluralsight教程“带Kotlin的Android应用程序:工具和测试”,并遇到了以下问题: 在仪器化测试期间,我们检查一个微调器和两个文本字段中的文本,在视频中它只是工作正常,但我遇到了问题。以下是我的测试代码: import android.provider.ContactsContract import android.support.test.runner.AndroidJUnit4 import org.junit.Assert.* import org.junit.

我正在关注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'
}
归功于