Hamcrest 关于两个值的断言
Hamcrest assertion on two values
我可以很容易地做一个有两种可能结果的断言:
assertThat(result, anyOf(true, false)); // just a sample, doesn't make sense as an assertion
但是,我需要断言我的结果之一等于某个值:
assertThat(result1 || result2, is(true));
以上有效,但错误消息没有说明哪个结果是 false
。 Hamcrest 中是否有类似以下内容的内容?
assertThat(anyOf(result1, result2), is(true)); // just a hypothetical assertion
你可以反过来写断言:
assertThat(true, anyOf(is(result1), is(result2)))
当 result1
或 result2
不是 true
时,这仍然会抛出一个断言错误,并且该消息将告诉最终值或 result1
和 result2
...在预期的部分,有点尴尬。
根据你的问题:
However, I need to perform assertion that one of my results is equal to some value:
这意味着您的实际用例是确定结果列表是否具有给定值。这可以清楚地表达为:
assertThat(Arrays.asList(result1, result2), hasItem(true));
这断言由两个结果形成的列表具有给定的项目。如果不是,断言错误将是:
Expected: a collection containing <true>
but: was <false>, was <false>
消息告诉您集合中每个元素的值。
我可以很容易地做一个有两种可能结果的断言:
assertThat(result, anyOf(true, false)); // just a sample, doesn't make sense as an assertion
但是,我需要断言我的结果之一等于某个值:
assertThat(result1 || result2, is(true));
以上有效,但错误消息没有说明哪个结果是 false
。 Hamcrest 中是否有类似以下内容的内容?
assertThat(anyOf(result1, result2), is(true)); // just a hypothetical assertion
你可以反过来写断言:
assertThat(true, anyOf(is(result1), is(result2)))
当 result1
或 result2
不是 true
时,这仍然会抛出一个断言错误,并且该消息将告诉最终值或 result1
和 result2
...在预期的部分,有点尴尬。
根据你的问题:
However, I need to perform assertion that one of my results is equal to some value:
这意味着您的实际用例是确定结果列表是否具有给定值。这可以清楚地表达为:
assertThat(Arrays.asList(result1, result2), hasItem(true));
这断言由两个结果形成的列表具有给定的项目。如果不是,断言错误将是:
Expected: a collection containing <true>
but: was <false>, was <false>
消息告诉您集合中每个元素的值。