如果您匹配列表或地图或任何其他复杂结构,那么查看给定内容与预期内容之间的区别非常有用。例如:
Map("a" -> 1, "b" -> 2, "c" -> 3) should equal Map("a" -> 1, "b" -> 5, "c" -> 3)
// ScalaTest output:
[info] Map("a" -> 1, "b" -> 2, "c" -> 3) did not equal Map("a" -> 1, "b" -> 5, "c" -> 3) (Test.scala)
[info] org.scalatest.exceptions.TestFailedException:
[info] ...
您必须手动浏览两个地图才能找到它们之间的差异,您的收藏越大,它就变得越难。
另一方面,在RSpec中你会得到:
expect({a: 1, b: 2, c: 3}).to match({a: 1, b: 5, c: 3})
// RSpec output:
Failure/Error: it { expect({a: 1, b: 2, c: 3}).to match({a: 1, b: 5, c: 3})}
expected {:a=>1, :b=>2, :c=>3} to match {:a=>1, :b=>5, :c=>3}
Diff:
@@ -1,4 +1,4 @@
:a => 1,
-:b => 5,
+:b => 2,
:c => 3,
# ./spec/test_spec.rb:2:in `block (2 levels) in <top (required)>'
是否可以使用ScalaTest进行类似的操作?
答案 0 :(得分:3)
我不相信有任何与你描述完全相同的东西。 ScalaTest中最接近的是String
的内置差异。
这不太理想,但在地图末尾添加toString
可以提供更好的输出。同样,在pretty
上使用org.scalautils.PrettyMethods
方法也会做同样的事情。
例如:
Map("a" -> 1, "b" -> 2, "c" -> 3).toString shouldBe Map("a" -> 1, "b" -> 5, "c" -> 3).toString
// scalatest output
[info] "Map(a -> 1, b -> [2], c -> 3)" was not equal to "Map(a -> 1, b -> [5], c -> 3)"
[info] org.scalatest.exceptions.TestFailedException: ...
此scalatest-users email thread向您提出类似问题,并由ScalaTest的作者Bill Venners作出回应。
答案 1 :(得分:0)
如果您使用 Scala 测试,您可能想尝试 diffx https://diffx-scala.readthedocs.io/en/latest/,它帮助我调试了几个大型测试用例。