适用于 Kotlin 多平台移动设备的 Hamcrest 和 Mockk

Qui*_*don 2 kotlin kotlin-multiplatform kotlin-multiplatform-mobile

我正在尝试在KMMshared项目的模块中编写测试。在共享模块的 build.gradle.kts 文件中,我有以下内容:

sourceSets {
    val commonMain by getting
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))


            //TODO: Hamcrest
            //implementation("junit:junit:4.12")
            //implementation("org.hamcrest:hamcrest-library:1.3")

            //TODO: Mockk
            //implementation("io.mockk:mockk:1.10.4")
        }
    }
    //...
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

implementation(kotlin("[library]"))
Run Code Online (Sandbox Code Playgroud)

得到相同的结果:IDE 不再识别测试,我无法运行它们。

Nag*_*obi 9

不幸的是,没有一个具有 K/N 支持的模拟库(AFAIK)。

这是Mockk 的 K/NMockk 的 K/JS问题以供将来参考,或者您也可以查看Touchlab 的 Karmok

对于 Hamcrest,请在此处查看他们的问题

编辑/更新

到了 2022 年,上述答案不再成立。Mockk现在支持共享模块中的模拟。请检查这里mockk.io添加以下依赖项,你应该可以开始了

testImplementation "io.mockk:mockk-common:{version}"

模拟体验是无缝的,就像常规的 Android 单元测试用例一样。