You can use CollectionAssert with shared collections. The trick is to understand that CollectionAssert methods work on ICollection , and although some common collection interfaces implement ICollection , List<T> does.
Thus, you can get around this limitation using the ToList extension ToList :
IEnumerable<Foo> expected = //... IEnumerable<Foo> actual = //... CollectionAssert.AreEqual(expected.ToList(), actual.ToList());
However, I still believe that CollectionAssert is broken in many other ways, so I prefer to use Assert.IsTrue with LINQ extension methods, for example:
Assert.IsTrue(expected.SequenceEquals(actual));
FWIW, I am currently using these extension methods to perform other comparisons:
public static class EnumerableExtension { public static bool IsEquivalentTo(this IEnumerable first, IEnumerable second) { var secondList = second.Cast<object>().ToList(); foreach (var item in first) { var index = secondList.FindIndex(item.Equals); if (index < 0) { return false; } secondList.RemoveAt(index); } return secondList.Count == 0; } public static bool IsSubsetOf(this IEnumerable first, IEnumerable second) { var secondList = second.Cast<object>().ToList(); foreach (var item in first) { var index = secondList.FindIndex(item.Equals); if (index < 0) { return false; } secondList.RemoveAt(index); } return true; } }
Mark Seemann Mar 14 2018-10-10T00: 00Z
source share