当验证阶收集的预期值,assertResult
方法简便:是否有可能使用assertResult检查equallity阵列中ScalaTest
"The list" should "be generated correctly" in {
assertResult(List(10, 20)) {
//Some code that should output
List(10, 20)
}
}
如果出现问题,会产生不错的错误信息:
Expected List(10, 20), but got List(10, 30)
不幸的是,它并不适用于阵列工作,因为==
运营商检查的身份,而不是平等的(这种行为背后的原因已经讨论了很多,比如这里:Why doesn't Array's == function return true for Array(1,2) == Array(1,2)?)。
所以,类似的检查数组生成以下错误消息:
Expected Array(10, 20), but got Array(10, 20)
原因,有可能使用should equal
匹配:
"The array" should "be generated correctly" in {
Array(10, 20) should equal {
//Some code that should output
Array(10, 20)
}
}
但IMO它不太方便,因为它更均等检查期望验证:
Array(10, 20) did not equal Array(10, 30)
是重新对ScalaTest中的数组进行断言检查,从而将预期结果与实际结果明确区分开来?
谢谢你的帮助! – Wildfire