使用多平台模拟 kotlin 中的常见测试

问题描述

我无法在 kotlin 多平台中使用通用模拟库 ( mockk.io )。在他们的网站上,它说要在 kotlin 多平台中使用 mockk,您只需将此行添加到您的 gradle 中。 testImplementation "io.mockk:mockk-common:{version}"

添加了它并且它正常构建,只有当我想使用它时它才会失败。给予

Unresolved reference: io
Unresolved reference: mockk

我的 gradle 文件


kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTarget.apply {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }
    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation("io.mockk:mockk-common:1.10.4")
            }
        }
    }
}

解决方法

除非发生某些变化,否则 mockk 在 Kotlin Native 上不起作用。