2016-08-12 15 views
3

我需要执行声明,指出集合包含来自其他集合的所有元素。在Spock中声明'containsAll'时显示更好的差异

下面的测试失败了,因为第一次收集不从第二个包含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]> 

什么样的断言将是斯波克惯用得到更好失败消息containsAll

+0

我认为斯波克有更好的支持比较字符串,所以你可以尝试'first.toString()== second.toString() '。当然这是一个肮脏的黑客... – injecteer

回答

4

我想你可以破解并做类似(right - left).isEmpty()这应该打印出正确的,但不是在左边的元素。

这是一个有点哈克的方式,但真的什么我可以拿出

3

我同意AssertJ具有更好的消息,你可以在斯波克测试使用AssertJ。

以外,你必须定义断言消息你自己喜欢

assert first.containsAll(second), "$first does not contain all from $second. Missing elements: " + (second - first)