流畅的断言。如何验证两个列表至少有一个相同的元素

Posted

技术标签:

【中文标题】流畅的断言。如何验证两个列表至少有一个相同的元素【英文标题】:FluentAssertion. How to verify that two lists have at least one the same element 【发布时间】:2017-09-10 11:01:41 【问题描述】:

默认情况下,我的预期和实际响应看起来像 JSON 对象。就我而言,有两个列表。我必须验证这两个列表在列表一中是否具有相同的元素。

函数应该是这样的:

(expectedResponse, actualResponse) => ((List<Question>)actualResponse.Body).Should()
                        .NotIntersectWith(((List<Question>)expectedResponse.Body))

【问题讨论】:

【参考方案1】:

从他们的wiki,你可以使用IntersectWith方法:

IEnumerable otherCollection = new[]  1, 2, 5, 8, 1 ;
IEnumerable anotherCollection = new[]  10, 20, 50, 80, 10 ;
collection.Should().IntersectWith(otherCollection);

确保集合中的对象正确实现IEquatable&lt;T&gt; 接口(可选)和Equals 方法(必需)。

【讨论】:

以上是关于流畅的断言。如何验证两个列表至少有一个相同的元素的主要内容,如果未能解决你的问题,请参考以下文章