tags:

views:

930

answers:

2

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

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

of course that's not the real method :) 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 ?

+3  A: 

xUnit.Net does not have this out of the box but authors provided sample of how to implement it by yourself: collection assert which references to CollectionExample.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: Collection Constraints (NUnit 2.4 / 2.5)

MbUnit also has CollectionAssert class with behavior very similar to NUnit: CollectionAssert class

Konstantin Spirin
Source code link changed for http://xunit.codeplex.com/SourceControl/changeset/view/d947e347c5c3#Samples%2fAssertExamples%2fCollectionExample.cs
VirtualBlackFox
+3  A: 

In the current version of XUnit (1.5) you can just use

Assert.Equal(expected, actual);

The above method will do an element by element comparison of the two lists. I'm not sure if this works for any prior version.

See the AssertComparer class in the source (http://xunit.codeplex.com/sourcecontrol/changeset/view/39038?projectName=xunit#115711 - right at the bottom).

hwiechers