Search code examples
c#.netfluent-assertions

How to write FluentAssertion for nested collections, order independent?


Using FluentAssertions 3.5.1, I'm trying to assert that a List of Integer arrays is equivalent to another list of integer arrays, without caring about item order. This is not working. In trying to break this problem down, I've tried to assert that they are equal when they do have the same order and that's not working either:

var a = new List <Int32[]> { new Int32[] { 1, 2 } };
var b = new List <Int32[]> { new Int32[] { 1, 2 } };

a.Should().BeEquivalentTo(b);

This gives me the message:

Expected collection {{1, 2}} to be equivalent to {{1, 2}}, but it misses {{1, 2}}.

Perhaps BeEquivalentTo is not the right assertion for comparing nested collections??

Michael


Solution

  • You can fix it by using next code:

    a.ShouldBeEquivalentTo(b);
    

    or

    a.ShouldAllBeEquivalentTo(b);
    

    It will work, because ShouldBeEquivalentTo is deep equals comparison and Should().BeEquivalentTo() isn't.