How do you create a Test that confirms a Toast is displayed in a fragment.
Here are the bones of my test
@RunWith(AndroidJUnit4:class)
class MyFragmentTest() {
val displayToast = R.id.btn_toast
@Before()
fun setup() {
launchFragmentInContainer<MyFragment>(null, R.style.My_Theme)
}
@Test
fun displayToast() {
onView(withId(displayToast).perform(click())
// Insert different ideas here to display toast.
}
}
- Idea one
onView(WithText("My Toast").check(matches(isDispalyed)))
Error
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131755008>[Toast_no_word] value: No word entered
- Idea 2 This example requires a custom ToastMatcher, ToastMatcher
onToast("My Toast").check(matches(isDisplayed()))
error
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "No word entered"
- Idea three This option also uses custom ToastMatcher, I've updated it for kotlin.
https://stackoverflow.com/a/42061993/3943340 Coding with Mitch, youtube tutorial
onView(withText("My Toast")).inRoot(TOastMatcher()).check(matches(isDisplayed()))
Returns the following error
androidx.test.espresso.NoMatchingRootException: Matcher 'is toast' did not match any of the following roots: [Root{application-window-token=android.view.ViewRootImpl$W@6b8a33b, window-e
I've been stuck on this for a while if you have an alternative suggestion or an idea of where I've gone wrong it would be apprecaited.