mockK 无法区分每个语句中的类型
mockK cannot differentiate types in every statements
我正在 Kotlin 中编写一个方法,其中 returns elasticsearch 索引具有分配给它们的别名:
fun getActiveIndices(cluster: ElasticsearchCluster): List<IndexModel> {
val aliases = elasticsearchCommandExecutor.execute(GetAllAliasesCommand(cluster))
val indices = elasticsearchCommandExecutor.execute(GetAllIndicesCommand(cluster))
indices.forEach{ it.active = aliases.any { alias -> it.name == alias.index } }
return indices.filter { !it.irregular && it.active }
}
其中 GetAllAliasesCommand
和 GetAllIndicesCommand
是 ElasticsearchCommand<T>
的子类。
我正在尝试使用 mockK:
测试此方法的行为
@Test
fun `getActiveIndices should make correct calls`() {
val aliases = listOf(.. A list of AliasModel)
val indices = listOf(.. A list of IndexModel)
every { elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>()) } returns aliases
every { elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>()) } returns indices
val result = indexService.getActiveIndices(ElasticsearchCluster.SOME_CLUSTER)
verify { elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>()) }
verify { elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>()) }
assert(result == listOf(.. A list of IndexModel))
}
问题是 mockK 无法区分 every
语句中的 any<GetAllIndicesCommand>()
和 any<GetAllAliasesCommand>()
所以 elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>())
和
elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>())
returns indices
。这意味着它应用最后一个 every
语句。有没有办法根据命令类型使其成为return?
原来我不得不为此目的使用 ofType 匹配器。所以最后的代码是:
@Test
fun `getActiveIndices should make correct calls`() {
val aliases = listOf(.. A list of AliasModel)
val indices = listOf(.. A list of IndexModel)
every { elasticsearchCommandExecutor.execute(ofType(GetAllAliasesCommand::class)) } returns aliases
every { elasticsearchCommandExecutor.execute(ofType(GetAllIndicesCommand::class)) } returns indices
val result = indexService.getActiveIndices(ElasticsearchCluster.SOME_CLUSTER)
verify { elasticsearchCommandExecutor.execute(ofType(GetAllIndicesCommand::class)) }
verify { elasticsearchCommandExecutor.execute(ofType(GetAllAliasesCommand::class)) }
assert(result == listOf(.. A list of IndexModel))
}
我正在 Kotlin 中编写一个方法,其中 returns elasticsearch 索引具有分配给它们的别名:
fun getActiveIndices(cluster: ElasticsearchCluster): List<IndexModel> {
val aliases = elasticsearchCommandExecutor.execute(GetAllAliasesCommand(cluster))
val indices = elasticsearchCommandExecutor.execute(GetAllIndicesCommand(cluster))
indices.forEach{ it.active = aliases.any { alias -> it.name == alias.index } }
return indices.filter { !it.irregular && it.active }
}
其中 GetAllAliasesCommand
和 GetAllIndicesCommand
是 ElasticsearchCommand<T>
的子类。
我正在尝试使用 mockK:
@Test
fun `getActiveIndices should make correct calls`() {
val aliases = listOf(.. A list of AliasModel)
val indices = listOf(.. A list of IndexModel)
every { elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>()) } returns aliases
every { elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>()) } returns indices
val result = indexService.getActiveIndices(ElasticsearchCluster.SOME_CLUSTER)
verify { elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>()) }
verify { elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>()) }
assert(result == listOf(.. A list of IndexModel))
}
问题是 mockK 无法区分 every
语句中的 any<GetAllIndicesCommand>()
和 any<GetAllAliasesCommand>()
所以 elasticsearchCommandExecutor.execute(any<GetAllIndicesCommand>())
和
elasticsearchCommandExecutor.execute(any<GetAllAliasesCommand>())
returns indices
。这意味着它应用最后一个 every
语句。有没有办法根据命令类型使其成为return?
原来我不得不为此目的使用 ofType 匹配器。所以最后的代码是:
@Test
fun `getActiveIndices should make correct calls`() {
val aliases = listOf(.. A list of AliasModel)
val indices = listOf(.. A list of IndexModel)
every { elasticsearchCommandExecutor.execute(ofType(GetAllAliasesCommand::class)) } returns aliases
every { elasticsearchCommandExecutor.execute(ofType(GetAllIndicesCommand::class)) } returns indices
val result = indexService.getActiveIndices(ElasticsearchCluster.SOME_CLUSTER)
verify { elasticsearchCommandExecutor.execute(ofType(GetAllIndicesCommand::class)) }
verify { elasticsearchCommandExecutor.execute(ofType(GetAllAliasesCommand::class)) }
assert(result == listOf(.. A list of IndexModel))
}