xUnit : Assert two List<T> are equal?

Petar Petrov picture Petar Petrov · Jan 7, 2009 · Viewed 61.6k times · Source

I'm new to TDD and xUnit so I want to test my method that looks something like:

List<T> DeleteElements<T>(this List<T> a, List<T> b);

Is there any Assert method that I can use ? I think something like this would be nice

List<int> values = new List<int>() { 1, 2, 3 };
List<int> expected = new List<int>() { 1 };
List<int> actual = values.DeleteElements(new List<int>() { 2, 3 });

Assert.Exact(expected, actual);

Is there something like this ?

Answer

Konstantin Spirin picture Konstantin Spirin · Jan 7, 2009

xUnit.Net recognizes collections so you just need to do

Assert.Equal(expected, actual); // Order is important

You can see other available collection assertions in CollectionAsserts.cs

For NUnit library collection comparison methods are

CollectionAssert.AreEqual(IEnumerable, IEnumerable) // For sequences, order matters

and

CollectionAssert.AreEquivalent(IEnumerable, IEnumerable) // For sets, order doesn't matter

More details here: CollectionAssert

MbUnit also has collection assertions similar to NUnit: Assert.Collections.cs