为使用withContext(Dispatchers.Main)测试方法的kotlin 挂起方法运行单元测试时失败,出现以下异常:
我的协程库版本是kotlinx-coroutines-core:1.1.1和kotlinx-coroutines-android:1.1.1
例子:
suspend fun methodToTest() {
withContext(Dispatchers.Main) {
doSomethingOnMainThread()
val data = withContext(Dispatchers.IO) {
doSomethingOnIOThread()
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,当我删除withContext(Dispatchers.Main)它时,它工作正常。
java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:79)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:54)
at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:373)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:25)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:152)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
Run Code Online (Sandbox Code Playgroud) 我在使用应用程序操作测试工具运行应用程序操作健身示例时遇到此问题,助手说“抱歉,我找不到”
步骤如下:
Android Material 3引入了海拔覆盖(色调颜色覆盖)。如果提供或增加,则会导致 MaterialCard 视图的背景颜色发生变化android:elevation,并影响原始背景颜色。
文档没有提供太多与样式或禁用高程覆盖相关的信息。
https://developer.android.com/jetpack/compose/designsystems/material3#elevation https://m3.material.io/styles/elevation/applying-elevation
android material-design material-components-android android-dark-theme material-design-3