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



我正在遵循 Pluralsight 教程"Android Apps with Kotlin:工具和测试",遇到了以下问题:

插桩测试期间,我们检查微调器和两个文本字段中的文本,在视频中它刚刚解决,但我遇到了问题。这是我的测试代码:

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 解决方案添加"包含字符串",但显然这不起作用。我做错了什么?

要解决此问题,您需要确保以下库具有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,Pluralsight同一课程的作者。

最新更新