Which method should I use to assert that two lists contains the same objects with MSpec?
Asked
Active
Viewed 1,343 times
2 Answers
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 – Roger Lipscombe Nov 28 '11 at 15:28`, and then uses `Comparer .Default`. -
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