我在commonTest -> shared模块中导入了->库。测试类中没有导入错误,但是当我运行测试时,会得到以下错误:
Unresolved reference: every
Unresolved reference: mockk
Unresolved reference: verify在我使用mock库方法的所有地方。造成这些错误的原因是什么?
在控制台中出现错误的测试示例:
class DefaultAppPreferenceStorageTest {
val appPreference = mockk<AppPreference>() //Unresolved reference: mockk
val jsonService = mockk<JsonService>() //Unresolved reference: mockk
val jsonKey = "key"
val value = 1
val stringValue = "$value"
val defaultIntValue = Random.nextInt()
val storage = DefaultAppPreferenceStorage(
appPreference,
jsonService
)
inner class PutJsonTest {
@BeforeTest
fun beforeEachTest() {
every { jsonService.mapToString(value) } returns stringValue //Unresolved reference: every
storage.putJson(jsonKey, value)
}
@Test
fun testPutJson() {
verify(verifyBlock = { jsonService.mapToString(value) }) //Unresolved reference: verify
verify(verifyBlock = { appPreference.putString(jsonKey, stringValue) }) //Unresolved reference: verify
}
}
...
}更新依赖项
const val mockk = "1.12.5"
const val mockk = "io.mockk:mockk-common:${Version.mockk}" val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(ShareTestDependencies.mockk)
implementation(ShareTestDependencies.coroutinesTest)
}
}发布于 2022-09-21 11:23:30
如果您使用KMP来构建本机目标(iOS、Linux、Windows等),则mockk不支持本机。它确实支持JVM和JS,并且有一个通用的源代码定义来支持这两个平台。IDE可能没有错误,因为有一个常见的mockk定义(尽管我从未尝试过)。
看看实际正在运行的测试任务。如果是针对一个本地目标,那肯定是发生了什么。
发布于 2022-10-05 15:13:42
在从mockk 1.12.5升级到1.13.2时,我看到了上述错误(未解决的引用: every和其他)。我可以通过向mockk添加依赖项(而不是现有的mockk依赖项或旁边的依赖项)来解决这个问题。
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>${mockk.version}</version>
</dependency>这个问题在前面已经解决了(参见https://github.com/mockk/mockk/issues/889),并假定已经解决了。显然,并非在所有情况下都是如此。
https://stackoverflow.com/questions/73799505
复制相似问题