NUnit CollectionConstraints exceptions
Pregunta
I'm using NUnit 2.5.6.10205 in a .NET 3.5 C# app. I'm using NUnit's Collection Constraint to assert if an IEnumerable is sorted by a paramter.
It doesn't seem to be working for me, as I'm getting an exception indicating that my actual value isn't an IEnumreable. allEntities is a List<T>
which implements IEnumerable<T>
. I believe that NUnit is looking for an IEnumerable
, not an IEnumerable<T>
, but IEnumerable<T>
implements IEnumerable
. Is this an issue of co / contra variance?
Assert.That(allEntities, Is.All.Ordered.By("CreationDate"));
.
System.ArgumentException : The actual value must be an IEnumerable
Parameter name: actual
Also, is there some way I can express the sort property using a Lambda? Using a literal string for the property makes it brittle.
Solución
There is no need for the All
, try:
Assert.That(allEntities, Is.Ordered.By("CreationDate"));
Otros consejos
I was using the All contstraint, but that is used to make assertions on each item in the list, i.e.
// checks that for each T in myList, that it is greater than 5
Assert.That(myList, Is.All.GreaterThan(5));
From NUnit: "Applies a constraint to each item in a collection, succeeding only if all of them succeed."
I want to test a property of the list itself, so I want:
// checks that the list itself is ordered by the property CreationDate
Assert.That(allEntities, Is.Ordered.By("CreationDate"));
Hopefully someone else will find this question / answer useful in the future.