當驗證階收集的預期值,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