Comparing Two Lists with MSpec

What method should I use to claim that two lists contain the same objects with MSpec?

+5
source share
2 answers

You can use the extension method ShouldContainOnly(IEnumerable<T>).

So, if you have 2 lists, listAand listBuse:

listA.ShouldContainOnly(listB)
+6
source

If the order of the items in the list does not matter, you should use

listA.ShouldContainOnly(listB); // both lists must have exactly the same items
listA.ShouldContain(listB);     // listA must at least contain the items of listB

If the order of the items matters, you can use

listA.ShouldEqual(listB);
+3
source

All Articles