Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that a collection is in the right order

Tags:

c#

How do I assert in MSTest that the order of the returned collection is correct?

[TestMethod]
    public void when_sorting_movies_it_should_be_able_to_sort_all_movies_by_title_descending()
    {
        populateTestMovies(movie_collection);
        MovieLibrary movieLibrary = new MovieLibrary(movie_collection);
        IEnumerable<Movie> results = movieLibrary.sort_all_movies_by_title_descending();
        Assert.IsTrue(results.Contains(theres_something_about_mary));
        Assert.IsTrue(results.Contains(the_ring));
        Assert.IsTrue(results.Contains(shrek));
        Assert.IsTrue(results.Contains(pirates_of_the_carribean));
        Assert.IsTrue(results.Contains(indiana_jones_and_the_temple_of_doom));
        Assert.IsTrue(results.Contains(cars));
        Assert.IsTrue(results.Contains(a_bugs_life));
        Assert.AreEqual(7, results.Count());
    }
like image 208
Dave Mateer Avatar asked Dec 02 '22 03:12

Dave Mateer


2 Answers

Create a hard-coded IEnumerable<string> with the movie titles in the expected order, pull the titles from the result collection and use SequenceEqual to check that they come in the same order (assuming your referred constants are Movie objects, and that Movie has a Title property):

IEnumerable<string> expected = new[] 
{ 
    theres_something_about_mary.Title, 
    the_ring.Title,
   /* and so on */ 
};
Assert.IsTrue(results.Select(m => m.Title).SequenceEqual(expected));
like image 175
Fredrik Mörk Avatar answered Dec 03 '22 19:12

Fredrik Mörk


http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.collectionassert.aspx

like image 31
thedev Avatar answered Dec 03 '22 18:12

thedev