5

I have a list, every single element should also show up in another list, but not necessarily in the same order.

I could probably do the assert with a foreach, like this

Assert.IsTrue(list1.Count == list2.Count);
foreach(var element in list1)
{
    Assert.IsTrue(list2.Count(e => e.Equals(element)) == 1);
}

I am looking for a way to do this with fluentAssertions. The elements are not necessarily Equal, but are Equivalent. It would probably be something like

list1.ShouldAll().BeEquivalentInAnyOrderTo(list2);

But I can't find anything that solves my problem that easily.

What is the simplest way to check if both lists contain equivalent elements in any order using FluentAssertions?

Kaito Kid
  • 983
  • 4
  • 15
  • 34
  • 1
    `list1.ShouldBeEquivalentTo(expectedList)` - will check that equivalency without strict for the order. For more extensebility you can play with second parameter – Fabio Aug 08 '17 at 18:53
  • 1
    Try using `list1.ShouldAllBeEquivalentTo(list2)`; – Nkosi Aug 08 '17 at 18:54

1 Answers1

8

You're not far off:

list1.Should().BeEquivalentTo(list2);

should work. From: https://fluentassertions.com/collections/

Jonas Nyrup
  • 2,376
  • 18
  • 25
user1777136
  • 1,746
  • 14
  • 28
  • It worked! I'm not sure why I was Under the impression that this would take into account the order. I'll have to go change the other unit tests that used this line when keeping the order was actually important... – Kaito Kid Aug 08 '17 at 19:04