gpt4 book ai didi

android - IntentsRule 已弃用 Espresso

转载 作者:行者123 更新时间:2023-12-05 03:42:43 35 4
gpt4 key购买 nike

我一直在使用 BaseTest Espresso 类是这样的:

abstract class BaseTest<T : Activity> {

val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

abstract fun getTestActivity(): IntentsTestRule<T>
abstract fun startIntentActivity()

@Rule
@JvmField
var activityTestRule = this.getTestActivity()

fun launchActivity(intent: Intent?) {
getTestActivity().launchActivity(intent)
}

@Before
fun setUp() {
startIntentActivity()
}

@After
fun tearDown() {
activityTestRule.finishActivity()
}
}

但现在我看到了 abstract fun getTestActivity(): IntentsTestRule<T>已弃用,我们应该使用 ActivityScenarioActivityScenarioRule我应该如何修改该类以使用新类?

最佳答案

通常,可以使用 Espresso 或 ActivityScenarioRule 提供 Intent:

ActivityScenarioRule(Intent startActivityIntent)
Constructs ActivityScenarioRule with a given intent.

在 Kotlin 中:

lateinit var scenario: ActivityScenario<SomeActivity>
val intent = Intent(ApplicationProvider.getApplicationContext(), SomeActivity::class.java)

@get:Rule
val activityRule = activityScenarioRule<SomeActivity>(intent)

@Test
fun someTest() {
scenario = rule.scenario
scenario.onActivity { activity ->
...
}
}

@After
fun cleanup() {
scenario.close()
}

关于android - IntentsRule 已弃用 Espresso,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67122525/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com