ScalaTest:使用 ShouldMatcher 检查序列的内容
在我的单元测试中,我想表达计算(结果)序列产生了预定义的结果值序列。但无需假设序列容器的实际实现类型。
我想清楚且不言自明地阐明我的意图。
如果我尝试使用 ScalaTest 的“ShouldMatchers”并编写
val Input22 = ...
calculation(Input22) should equal (Seq("x","u"))
...那么我就会遇到简单的等式的麻烦,因为 calculation(..)
可能会返回一个 ArrayBuffer,而 Seq("x","u")
是一个 List
In my unit test, I want to express that a computed (result) sequence yielded a predefined sequence of result values. But without assuming anything about the actual implementation type of the sequence container.
And I want to spell out my intent rather clear and self-explanatory.
If I try to use the "ShouldMatchers" of ScalaTest and write
val Input22 = ...
calculation(Input22) should equal (Seq("x","u"))
...then I get into trouble with the simple equality, because calculation(..)
might return an ArrayBuffer, while Seq("x","u")
is an List
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你用的是2.7.7吗?在 2.8 中,具有相等元素(相同顺序)的不同 Seq 应该相等:
2.8 中这一规则的一个例外是数组,它只能与其他数组相等,因为它们是 Java 数组。 (当您对 Java 数组调用 .equals 时,它们不会在结构上进行比较,但 ScalaTest 匹配器确实会对两个数组进行结构相等。)
Are you using 2.7.7? In 2.8 different Seq's with the equal elements (in the same order) should be equal:
The one exception to this rule in 2.8 is arrays, which can only be equal to other arrays, because they are Java arrays. (Java arrays are not compared structurally when you call .equals on them, but ScalaTest matchers does do structural equality on two arrays.)