Parkyster
Parkyster

Reputation: 155

N Unit Alphabetical Order Assertion

I have a search box on a website that returns search results, based on keyword (storing these as a list in c#)

There are filter options which I need to test, one of which is product name A-Z.

When this is selected, the search results should be sorted accordingly.

Is there anyway to assert that this has been done against the list with N Unit ?

Upvotes: 1

Views: 916

Answers (2)

Patrick Quirk
Patrick Quirk

Reputation: 23757

If you just need to ensure they are sorted, use the constraint assertion syntax. It's as simple as this:

string[] yourActualSearchResults = new string[] ( "alpha", "beta", "gamma" );

Assert.That(yourActualSearchResults, Is.Ordered);

Upvotes: 1

k.m
k.m

Reputation: 31464

Yes, you can use CollectionAsserts which succeed if the two collections contain the same objects, in the same order. Of course this requires you to have expected collection sorted, like:

var expected = new[] { "A", "B", "C" };
var actual = ...
CollectionAssert.AreEqual(expected, actual);

Similar effect can be achieved with constraint model and Enumerable.SequenceEqual:

var expected = new[] { "A", "B", "C" };
var actual = ...
Assert.That(expected.SequenceEqual(actual), Is.True);

But this will have less localized failure message (true vs false), rather than pointing where collections differ (which is the case for CollectionAssert).

Upvotes: 0

Related Questions