作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在调查我的 Android 应用程序中的协程测试并遵循此代码实验室 Advanced Android in Kotlin 05.3: Testing Coroutines and Jetpack integrations
此 codelab 包含以下代码 fragment
@ExperimentalCoroutinesApi
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
@ExperimentalCoroutinesApi
@Before
fun setupDispatcher() {
Dispatchers.setMain(testDispatcher)
}
@ExperimentalCoroutinesApi
@After
fun tearDownDispatcher() {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
但是 TestCoroutineDispatcher 被标记为已弃用,并带有以下注释:-
/**
* [CoroutineDispatcher] that performs both immediate and lazy execution of coroutines in tests
* and uses a [TestCoroutineScheduler] to control its virtual clock.
*
* By default, [TestCoroutineDispatcher] is immediate. That means any tasks scheduled to be run without delay are
* immediately executed. If they were scheduled with a delay, the virtual clock-time must be advanced via one of the
* methods on the dispatcher's [scheduler].
*
* When switched to lazy execution using [pauseDispatcher] any coroutines started via [launch] or [async] will
* not execute until a call to [DelayController.runCurrent] or the virtual clock-time has been advanced via one of the
* methods on [DelayController].
*
* @see DelayController
*/
@Deprecated("The execution order of `TestCoroutineDispatcher` can be confusing, and the mechanism of " +
"pausing is typically misunderstood. Please use `StandardTestDispatcher` or `UnconfinedTestDispatcher` instead.",
level = DeprecationLevel.WARNING)
// Since 1.6.0, ERROR in 1.7.0 and removed as experimental in 1.8.0
public class TestCoroutineDispatcher(public override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler()):
TestDispatcher(), Delay, SchedulerAsDelayController
{...}
不清楚我应该如何使用
TestCoroutineDispatcher()
的建议替代品。是
StandardTestDispatcher
和
UnconfinedTestDispatcher
TestCoroutineDispatcher() 的直接替代品?
最佳答案
似乎可以使用更优雅的解决方案,如 runTest()
自从 1.6.0
取自此 SO answer
见documentation有关如何使用该模块的详细信息。
关于使用 TestCoroutineDispatcher(已弃用)替代方案对 Kotlin 协程进行 Android 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71464816/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!