【问题标题】:Toast not showing in espresso test意式浓缩咖啡测试中未显示吐司
【发布时间】:2020-05-15 14:56:57
【问题描述】:

我想使用 Espresso 测试 Toast 消息,但是当我调用显示 toast 的方法时,没有显示 toast。 它在我正常运行应用程序时显示,但是当我运行测试时,没有显示 toast。

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

我在 TestFragment 的 showMessage 方法中调用 showMessage(),该方法在 FragmentScenario 的 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()))
}

测试用例卡在了

check(matches(isDisplayed()))

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

【问题讨论】:

  • 我现在遇到了这个问题。你找到解决办法了吗?

标签: android testing android-espresso instrumentation android-instrumentation


【解决方案1】:

将 espresso 断言移到 onFragment() 之外。

scenario.onFragment { it.showMessage(R.string.test_string) }
onView(withText(R.string.test_string))
     .inRoot(ToastMatcher())
     .check(matches(isDisplayed()))

ps:withText() 接受 String 资源 ID。

【讨论】:

  • 不。那是行不通的。实际上,我认为主要问题是它没有显示任何吐司消息。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多