57

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!

Samuel Neugber
  • 1,041
  • 1
  • 11
  • 17

8 Answers8

109

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.

DLeh
  • 23,806
  • 16
  • 84
  • 128
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • 3
    Perfect, exactly what I was hoping for. Thanks for introducing me to the options parameter! :) – Samuel Neugber Oct 10 '14 at 11:03
  • 13
    Took me too long to realize `Should().BeEquivalentTo()` apparently isn't the same as `ShouldBeEquivalentTo()`.... – RJB Aug 23 '17 at 23:45
  • 1
    I'm fixing this in 5.0. See https://github.com/fluentassertions/fluentassertions/pull/593 – Dennis Doomen Aug 25 '17 at 14:08
  • Any particular reason NotBeEquivalentTo doesn't have an overload with options? I need to verify that two enumerables are equivalent, but not in the same order. – Toby Aug 20 '19 at 13:51
17

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. Hope this helps someone

Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76
Marc Ziss
  • 659
  • 6
  • 10
  • 3
    With the latest fluentassertions nuget, you have to use it as follows: actualRows.Should().BeEquivalentTo(expectedRows,options => options.WithStrictOrdering()); – thomasgalliker May 13 '20 at 20:12
6

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();
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
4

From this post.

The newer ShouldBeEquivalentTo() introduced in FA 2.0 is doing an in-depth structural comparison and also reporting on any differences

You can achieve it in this way.

actual.Should().BeEquivalentTo(expectation, c => c.WithStrictOrdering());
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • 1
    Just noting that one can configure FluentAssertions to use this option by default: https://fluentassertions.com/tips/#using-global-assertionoptions – Frank Schwieterman May 20 '23 at 00:29
1

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
gmn
  • 4,199
  • 4
  • 24
  • 46
  • Already tried that, doesn't care about the order of elements. Edit -> That was my thought as well, I was just hoping I had overlooked a function somewhere. – Samuel Neugber Oct 10 '14 at 09:39
  • Maybe try SequenceEqual and a custom equality comparer then. – gmn Oct 10 '14 at 09:42
1

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

Jonas Nyrup
  • 2,376
  • 18
  • 25
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

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:

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

OR

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

OR

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

OR

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

Vergil C.
  • 1,046
  • 2
  • 15
  • 28
0

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.

Orace
  • 7,822
  • 30
  • 45