FluentAssertions: equivalence of sorted lists

2019-04-18 01:55发布

I'm trying to establish equivalence of two lists using FluentAssertions in C#, where two things are of importance:

  1. the elements are compared by the values they hold, not by reference (i.e. 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?

Cheers!

7条回答
Explosion°爆炸
2楼-- · 2019-04-18 02:32

You want the ShouldAllBeEquivalentTo method, that should compare the values of the properties of the two object graphs in a list.

*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.

查看更多
太酷不给撩
3楼-- · 2019-04-18 02:36

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

查看更多
放我归山
4楼-- · 2019-04-18 02:41

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. If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter.

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.

查看更多
太酷不给撩
5楼-- · 2019-04-18 02:44

I believe no. But you can always write an extension methon for that purpose. I.e.

    public static void AreEqual<T>(this IList<T> left, IList<T> right)
    {
        Assert.AreEqual(left.Count, right.Count);

        for (int i = 0; i < left.Count - 1; i++)
        {
            Assert.IsTrue(left[i].Equals(right[i]));
        }
    }
查看更多
时光不老,我们不散
6楼-- · 2019-04-18 02:45

The Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert class may have a method responding to your needs.

CollectionAssert.AreEqual Method (ICollection, ICollection, IComparer) should do the trick.

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.

查看更多
▲ chillily
7楼-- · 2019-04-18 02:48

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)

if this is not the case then you need to implement your own EqualityComparer for the objedts in the lists then use:

myObject.List.SequenceEqual(myOtherObject.ListToCompare, myEqualityComparer)
             .Should().BeTrue();
查看更多
登录 后发表回答