使用 Espresso 对在测试的 onCreate 期间启动的 Intents 进行存根 activity
Using Espresso to stub Intents started during the onCreate of the tested activity
我正在测试在 onCreate()
期间启动另一个 activity 的 activity。第二个 activity 以 startActivityForResult()
开始,主 activity 然后等待 onActivityResult()
.
我正在尝试使用 Espresso 对此进行测试,尝试将第二个 activity 与 intending()
打桩,并使用 intended()
.
验证它是否发生
尽管 espresso-intents 似乎不适用于从 onCreate()
方法 (see the warning in the last paragraphs here) 中启动的意图。
有没有人设法对从 onCreate()
中启动的 Intent 进行存根,如果是,怎么做的?
我能够通过使用以下 Kotlin 代码为自己工作:
@Rule @JvmField
val activityRule: IntentsTestRule<MainActivity> =
object : IntentsTestRule<MainActivity>(MainActivity::class.java, true, false) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
Intents.init()
intending(hasComponent(LaunchedFromOnCreateActivity::class.java.name)).respondWith(Instrumentation.ActivityResult(RESULT_OK, null))
}
override fun afterActivityLaunched() {
Intents.release()
super.afterActivityLaunched()
}
}
一般的想法是,由于生命周期的事情发生在 beforeActivityLaunched 和 afterActivityLaunched 之间,您需要在那里设置您的 intending。也就是说,这无法进行 预期 测试。
我正在测试在 onCreate()
期间启动另一个 activity 的 activity。第二个 activity 以 startActivityForResult()
开始,主 activity 然后等待 onActivityResult()
.
我正在尝试使用 Espresso 对此进行测试,尝试将第二个 activity 与 intending()
打桩,并使用 intended()
.
尽管 espresso-intents 似乎不适用于从 onCreate()
方法 (see the warning in the last paragraphs here) 中启动的意图。
有没有人设法对从 onCreate()
中启动的 Intent 进行存根,如果是,怎么做的?
我能够通过使用以下 Kotlin 代码为自己工作:
@Rule @JvmField
val activityRule: IntentsTestRule<MainActivity> =
object : IntentsTestRule<MainActivity>(MainActivity::class.java, true, false) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
Intents.init()
intending(hasComponent(LaunchedFromOnCreateActivity::class.java.name)).respondWith(Instrumentation.ActivityResult(RESULT_OK, null))
}
override fun afterActivityLaunched() {
Intents.release()
super.afterActivityLaunched()
}
}
一般的想法是,由于生命周期的事情发生在 beforeActivityLaunched 和 afterActivityLaunched 之间,您需要在那里设置您的 intending。也就是说,这无法进行 预期 测试。