浓咖啡测试中未显示吐司

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

我想使用Espresso测试Toast消息,但是当我调用显示Toast的方法时,则不显示Toast。它显示了我正常运行该应用程序的时间,但是当我运行测试时,没有显示吐司。

fun Context.showMessage(@StringRes message:Int){
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

我正在FragmentScenario的showMessage()中调用的TestFragment的showMessage方法中调用[C0

onFragment
override fun showMessage(message: Int) {
    context?.showMessage(message)
}

测试用例只是停留在

val scenario = launchFragmentInContainer<TestFragment>()
scenario.onFragment {
     it.showMessage(R.string.test_string)
     onView(withText(it.context.getString(R.string.test_string))
          .inRoot(ToastMatcher())
          .check(matches(isDisplayed()))
}

我猜是因为没有显示吐司。

android testing android-espresso instrumentation android-instrumentation
1个回答
0
投票

将浓缩咖啡声明移至check(matches(isDisplayed())) 之外。

onFragment()

ps:scenario.onFragment { it.showMessage(R.string.test_string) } onView(withText(R.string.test_string)) .inRoot(ToastMatcher()) .check(matches(isDisplayed())) 接受withText()资源ID。

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