moo*_*lin 2 unit-testing kotlin kotest
我有两个具有一些共同特征的对象,我想对其进行比较
data class Pet(val colour: String, val owner: Human, val legCount: Int)
data class Car(val colour: String, val owner: Human, val isElectric: Boolean)
Run Code Online (Sandbox Code Playgroud)
我想断言 a 中的某些元素List<Car>包含与给定 具有相同颜色和所有者的元素Pet。
这是一个假代码示例来说明我想要做什么:
cars.containsElementSatisfying { car ->
pet.colour shouldBe car.colour
pet.owner shouldBe car.owner
}
Run Code Online (Sandbox Code Playgroud)
我该如何使用 kotest 来做到这一点?我可以使用assertJ 的anySatisfy 断言来完成所需的功能。
我找到了解决方案。“Inspectors”kotest库包含许多与assertJ的anySatisfy断言类似的函数,特别是集合函数forAtLeastOne和forAtLeast(k)
这是用法示例:
cars.forAtLeastOne { car ->
pet.colour shouldBe car.colour
pet.owner shouldBe car.owner
}
Run Code Online (Sandbox Code Playgroud)
https://kotest.io/docs/assertions/inspectors.html