Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting in NUnit that a collection is in the same order as an expected collection

I know how to check that a collection is ordered by some property:

Assert.That(actual, Is.Ordered.By("Foo"));

How can I assert that actual contains the elements (1,2,5,3,4) in this specific order (without writing a custom comparer).

like image 664
ripper234 Avatar asked Jun 09 '09 15:06

ripper234


1 Answers

Use

CollectionAssert.AreEqual(expectedIEnumerable, actualIEnumerable);

This checks that the items are equal and are in the same order.

I'm fairly sure that when you use Assert.That on a collection, you get collection assert functionality. So you can say stuff like

Assert.That(collection, Is.EqualTo(expectedCollection)); // Same order

or

Assert.That(collection, Is.EquivalentTo(expectedCollection)); // Same item count

as well as stuff like

Assert.That(collection, Has.Count.EqualTo(expectedSize));

The Has keyword opens you up to the stuff that was specific to collection asserts, and is really useful.

like image 57
Mark Dickinson Avatar answered Oct 19 '22 09:10

Mark Dickinson