我需要执行断言,即集合包含来自其他集合的所有元素。
以下测试应该失败,因为第一个集合不包含来自第二个集合的7:
def first = [6, 1, 5, 2, 4, 3]
def second = [3, 4, 2, 5, 7, 6]
expect:
first.containsAll(second)然而,测试失败是根本无法读懂的。尚不清楚是否只缺少了7:
left.containsAll(right)
| | |
| false [3, 4, 2, 5, 7, 6]
[6, 1, 5, 2, 4, 3]AssertJ处理这个问题要好得多:
java.lang.AssertionError:
Expecting:
<[6, 1, 5, 2, 4, 3]>
to contain:
<[3, 4, 2, 5, 7, 6]>
but could not find:
<[7]>什么样的断言在Spock中是惯用的,以便为containsAll 获得更好的失败消息
发布于 2016-08-12 08:30:35
我想你可以黑进去做一些像(right - left).isEmpty()这样的事情,它应该打印出右边的元素,而不是左边的元素。
这是一种有点烦人的方式,但实际上我能想出的任何东西
发布于 2016-08-12 10:06:47
我同意AssertJ有更好的消息,您可以在Spock测试中使用AssertJ。
除此之外,您还必须定义您自己喜欢的断言消息。
assert first.containsAll(second), "$first does not contain all from $second. Missing elements: " + (second - first)https://stackoverflow.com/questions/38912559
复制相似问题