如何在片段中测试 Toast
How to test a Toast in a fragment
如何创建确认 Toast 显示在片段中的测试。
这是我测试的骨骼
@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.
}
}
- 想法一[=43=]
onView(WithText("My Toast").check(matches(isDispalyed)))
错误
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131755008>[Toast_no_word] value: No word entered
- 想法二
这个例子需要一个自定义的 ToastMatcher,
onToast("My Toast").check(matches(isDisplayed()))
错误
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "No word entered"
- 思路三
此选项还使用自定义 ToastMatcher,我已经为 kotlin 更新了它。
Coding with Mitch, youtube tutorial
onView(withText("My Toast")).inRoot(TOastMatcher()).check(matches(isDisplayed()))
Returns下面的错误
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
我已经坚持了一段时间,如果您有其他建议或对我哪里出错的想法,我会很感激。
使用 RobolectricTestRunner 在测试中启动片段 Activity。要启动片段,请在 @Before 函数中创建一个 FragmentScenario。在 @Test 中使用 ShadowToast 来测试吐司。
@RunWith(RobolecticTestRunner::class)
class MyFragmentUnitTest {
private lateinit var scenario: FragmentScenario<MyFragment>
@Before
fun setup() {
val viewModel: MyViewModel = mock(MyViewModel::class.java)
// Your scenario may not need to include all this information.
scenario = launchFragmentInContainer(
factory = MyFragmentFactory(viewModel),
fragmentArgs = null,
themeResId = R.style.Theme_mine
)
}
@Test
fun `show toast`() {
// Reference the button that will display the toast.
val toastButton = onView(ViewMatchers.withId(R.id.button_toast))
// perform a click on the button
toastButton.perform(ViewActionsclick())
val toastText = "This is my toast"
assertEquals(ShadowToast.shoedToast(toastText))
}
}
如何创建确认 Toast 显示在片段中的测试。
这是我测试的骨骼
@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.
}
}
- 想法一[=43=]
onView(WithText("My Toast").check(matches(isDispalyed)))
错误
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131755008>[Toast_no_word] value: No word entered
- 想法二
这个例子需要一个自定义的 ToastMatcher,
onToast("My Toast").check(matches(isDisplayed()))
错误
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "No word entered"
- 思路三 此选项还使用自定义 ToastMatcher,我已经为 kotlin 更新了它。
onView(withText("My Toast")).inRoot(TOastMatcher()).check(matches(isDisplayed()))
Returns下面的错误
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
我已经坚持了一段时间,如果您有其他建议或对我哪里出错的想法,我会很感激。
使用 RobolectricTestRunner 在测试中启动片段 Activity。要启动片段,请在 @Before 函数中创建一个 FragmentScenario。在 @Test 中使用 ShadowToast 来测试吐司。
@RunWith(RobolecticTestRunner::class)
class MyFragmentUnitTest {
private lateinit var scenario: FragmentScenario<MyFragment>
@Before
fun setup() {
val viewModel: MyViewModel = mock(MyViewModel::class.java)
// Your scenario may not need to include all this information.
scenario = launchFragmentInContainer(
factory = MyFragmentFactory(viewModel),
fragmentArgs = null,
themeResId = R.style.Theme_mine
)
}
@Test
fun `show toast`() {
// Reference the button that will display the toast.
val toastButton = onView(ViewMatchers.withId(R.id.button_toast))
// perform a click on the button
toastButton.perform(ViewActionsclick())
val toastText = "This is my toast"
assertEquals(ShadowToast.shoedToast(toastText))
}
}