ripper234
ripper234

Reputation: 230206

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).

Upvotes: 14

Views: 6439

Answers (1)

Mark Dickinson
Mark Dickinson

Reputation: 6633

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.

Upvotes: 29

Related Questions