如何使用 IntentsTestRule 和 launchFragmentInContainer 测试从片段发送的意图
Posted
技术标签:
【中文标题】如何使用 IntentsTestRule 和 launchFragmentInContainer 测试从片段发送的意图【英文标题】:How to test that an intent was sent from a fragment using IntentsTestRule and launchFragmentInContainer 【发布时间】:2020-06-27 04:44:06 【问题描述】:我有一个片段可以启动一个有意图的活动。我想使用Espresso UI
为此编写一个单元测试。我知道 Espresso 包含一个名为 IntentsTestRule
的用于测试意图的类。这条规则的唯一问题是它只适用于活动而不是片段,因为它的构造函数不接受 Fragment
类型的子类型。
@RunWith(androidJUnit4::class)
@Config(application = TestApplication::class)
class MyFragmentSpec
@Test
fun `On next button clicked send intent to start activity`()
launchFragmentInContainer<MyFragment>()
onView(withId(R.id.edit_text)).perform(typeText("HelloWorld\n"))
onView(withId(R.id.next_button)).perform(click())
Thread.sleep(2000L)
intended(hasAction("com.my.application.randomactitivy"))
我的测试失败,出现以下异常
com.my.application.MyFragmentSpec > On next button clicked send intent to start activity FAILED
java.lang.NullPointerException
at androidx.test.espresso.intent.Intents$2.check(Intents.java:194)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:425)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:288)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:272)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.$$robo$$android_os_Handler$handleCallback(Handler.java:873)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$dispatchMessage(Handler.java:99)
at android.os.Handler.dispatchMessage(Handler.java)
at org.robolectric.shadows.ShadowLegacyMessageQueue.dispatchMessage(ShadowLegacyMessageQueue.java:157)
at org.robolectric.shadows.ShadowLegacyMessageQueue.access$200(ShadowLegacyMessageQueue.java:42)
at org.robolectric.shadows.ShadowLegacyMessageQueue$1.run(ShadowLegacyMessageQueue.java:135)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:359)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:163)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:152)
at org.robolectric.shadows.ShadowLegacyMessageQueue.enqueueMessage(ShadowLegacyMessageQueue.java:142)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.$$robo$$android_os_Handler$enqueueMessage(Handler.java:745)
at android.os.Handler.enqueueMessage(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageAtTime(Handler.java:697)
at android.os.Handler.sendMessageAtTime(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageDelayed(Handler.java:667)
at android.os.Handler.sendMessageDelayed(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$post(Handler.java:395)
at android.os.Handler.post(Handler.java)
at androidx.test.espresso.base.BaseLayerModule$1.execute(BaseLayerModule.java:92)
at androidx.test.espresso.ViewInteraction.postAsynchronouslyOnUiThread(ViewInteraction.java:312)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:294)
at androidx.test.espresso.intent.Intents.intended(Intents.java:190)
at androidx.test.espresso.intent.Intents.intended(Intents.java:170)
at com.my.application.MyFragmentSpec.On next button clicked send intent to start activity(MyFragmentSpec.kt:107)
【问题讨论】:
【参考方案1】:我知道我迟到了,但我遇到了同样的问题
因为 launchFragmentInContainer 在 EmptyFragmentActivity 的实例中启动片段 我尝试了以下方法,它对我有用
@get:Rule
val intentsTestRule = IntentsTestRule(FragmentScenario.EmptyFragmentActivity::class.java)
希望对你有帮助
【讨论】:
这里有同样的问题,这个答案对我有用。以上是关于如何使用 IntentsTestRule 和 launchFragmentInContainer 测试从片段发送的意图的主要内容,如果未能解决你的问题,请参考以下文章
Debian 放弃了对 .la 文件的支持;我现在如何构建?
视野丨Klipsch La Scala三分频家庭影院是如何诞生的
如何在 Jetpack Compose 中实现现代 Card 组件(a'la Material Design 3.0)
csharp 如何使用continue运算符的示例。来自Microsoft C#Reference。 https://docs.microsoft.com/en-us/dotnet/csharp/la
csharp 如何使用continue运算符的示例。来自Microsoft C#Reference。 https://docs.microsoft.com/en-us/dotnet/csharp/la