5

Which method should I use to assert that two lists contains the same objects with MSpec?

W3Max
  • 3,328
  • 5
  • 35
  • 61

2 Answers2

6

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

So if you have 2 lists, listA and listB use:

listA.ShouldContainOnly(listB)
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
  • ...but check that your objects implement `Equals` appropriately. Machine.Specifications looks for `IComparable`, `IComparable`, `IEquatable`, and then uses `Comparer.Default`. – Roger Lipscombe Nov 28 '11 at 15:28
  • will that include order of the items check? – Sly Nov 12 '15 at 09:18
4

If the order of the items in the list doesn't matter, you would 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);
bitbonk
  • 48,890
  • 37
  • 186
  • 278