vendredi 15 mai 2020

Toast not showing in espresso test

I wanted to test a Toast message using Espresso, but when I am calling the method to show toast, the toast is not showing. It shows when I run the app normally, but when I run the test, no toast shows up.

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

I am calling showMessage() in the TestFragment's showMessage method which is called in FragmentScenario's 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()))
}

The test case is just stuck at

check(matches(isDisplayed()))

and I guess its because there is no toast displayed.

Aucun commentaire:

Enregistrer un commentaire