简体   繁体   English

FluentAssertions:排序列表的等价性

[英]FluentAssertions: equivalence of sorted lists

I'm trying to establish equivalence of two lists using FluentAssertions in C#, where two things are of importance:我试图在 C# 中使用 FluentAssertions 建立两个列表的等价性,其中有两件事很重要:

  1. the elements are compared by the values they hold, not by reference (ie they are equivalent, not equal)元素通过它们持有的值进行比较,而不是通过引用(即它们相等,不相等)
  2. the order of the elements in the lists is important列表中元素的顺序很重要

Is there no function in FluentAssertions (or even NUnit) that does this? FluentAssertions(甚至 NUnit)中是否没有执行此操作的函数?

Cheers!干杯!

By default, ShouldBeEquivalentTo() will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order.默认情况下, ShouldBeEquivalentTo()将忽略集合中的顺序,因为在大多数情况下,如果两个集合以任何顺序包含相同的项目,则它们是等效的。 If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter.如果您确实关心顺序,只需在options =>参数上使用WithStrictOrdering()的重载之一。

Example:例子:

var myList = Enumerable.Range(1, 5);
var expected = new[]
{
    1, 2, 3, 4, 5
};

//succeeds
myList.ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

//fails
myList.Reverse().ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());

Read more about these options in the documentation .文档中阅读有关这些选项的更多信息。

Late to the game here but I use the Fluent Assertions version of this here :晚这里的比赛,但我用的这个流利断言版本在这里

actualRows.Should().BeEquivalentTo(expectedRows,options => options.WithStrictOrdering());

It will check all the values of all the properties for equivalence, and with this option, the order matters.它将检查所有属性的所有值是否相等,并且使用此选项,顺序很重要。 If the order does not matter, omit the options param and it will make sure the item from one collection will exist somewhere in the other.如果顺序无关紧要,请省略 options 参数,它将确保一个集合中的项目将存在于另一个集合中的某处。 Hope this helps someone希望这有助于某人

I think you can just do:我认为你可以这样做:

myObject.List.SequenceEqual(myOtherObject.ListToCompare).Should().BeTrue();

This will only work if the elements in the list are equal when using Object.Equal(element1, element2)这仅在使用Object.Equal(element1, element2)时列表中的元素相等时才有效

if this is not the case then you need to implement your own EqualityComparer for the objedts in the lists then use:如果不是这种情况,那么您需要为列表中的对象实现自己的 EqualityComparer,然后使用:

myObject.List.SequenceEqual(myOtherObject.ListToCompare, myEqualityComparer)
             .Should().BeTrue();

From this post.这个帖子。

The newer ShouldBeEquivalentTo() introduced in FA 2.0 is doing an in-depth structural comparison and also reporting on any differences FA 2.0引入的较新的ShouldBeEquivalentTo()正在进行深入的结构比较,并报告任何差异

You can achieve it in this way.你可以通过这种方式实现它。

actual.Should().BeEquivalentTo(expectation, c => c.WithStrictOrdering());

You want the ShouldAllBeEquivalentTo method, that should compare the values of the properties of the two object graphs in a list.您需要 ShouldAllBeEquivalentTo 方法,该方法应该比较列表中两个对象图的属性值。

*Edit: I'd probably use the Linq Sequence equal with a custom equality comparer that uses ShouldBeEquivalentTo to care about the order of the elements. *编辑:我可能会使用 Linq Sequence equal 和自定义相等比较器,该比较器使用 ShouldBeEquivalentTo 来关心元素的顺序。

During my struggle with similar task found out about next method:在我与类似任务的斗争中,我发现了下一种方法:

IEnumerable collection = new[] { 1, 2, 5, 8 };

collection
    .Should()
    .ContainInOrder(new[] { 1, 5, 8 });

Collection Assertions Docs集合断言文档

For part 2 of this question, checking the order of the elements in a collection, as of 2020 (not sure which version this was introduced, am using v5.10.3 currently) you can use:对于此问题的第 2 部分,检查集合中元素的顺序,截至 2020 年(不确定这是引入的哪个版本,目前使用的是 v5.10.3)您可以使用:

mySimpleCollection.Should().BeInDescendingOrder() or myComplexCollection.Should().BeInDescendingOrder(x => x.SomeProperty) mySimpleCollection.Should().BeInDescendingOrder()myComplexCollection.Should().BeInDescendingOrder(x => x.SomeProperty)

OR或者

mySimpleCollection.Should().BeInAscendingOrder() or myComplexCollection.Should().BeInAscendingOrder(x => x.SomeProperty) mySimpleCollection.Should().BeInAscendingOrder()myComplexCollection.Should().BeInAscendingOrder(x => x.SomeProperty)

OR或者

mySimpleCollection.Should().NotBeInAscendingOrder() or myComplexCollection.Should().NotBeInAscendingOrder(x => x.SomeProperty) mySimpleCollection.Should().NotBeInAscendingOrder()myComplexCollection.Should().NotBeInAscendingOrder(x => x.SomeProperty)

OR或者

mySimpleCollection.Should().NotBeInDescendingOrder() or myComplexCollection.Should().NotBeInDescendingOrder(x => x.SomeProperty) mySimpleCollection.Should().NotBeInDescendingOrder()myComplexCollection.Should().NotBeInDescendingOrder(x => x.SomeProperty)

The Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert class may have a method responding to your needs. Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert类可能具有响应您需要的方法。

CollectionAssert.AreEqual Method (ICollection, ICollection, IComparer) should do the trick. CollectionAssert.AreEqual 方法(ICollection、ICollection、IComparer)应该可以解决问题。

Two collections are equal if they have the same elements in the same order and quantity.如果两个集合具有相同顺序和数量的相同元素,则它们相等。 Elements are equal if their values are equal, not if they refer to the same object.如果元素的值相等,则元素相等,而不是引用同一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM