Asserting in MSTEST that a collection OrderBy worked properly

  Uncategorised

Good morning. I am writing a unit test to validate that my API handler is sorting the collection properly. As you can see, it’s mocked data and I intentionally created the test data out of order to test the OrderBy functionality. Once the handler returns the collection I want to validate that it’s in the proper order, namely by date for this particular test. Here’s the code:

[TestMethod]
        public async Task GetByDay_ReturnsDtoList_WhenFound()
        {
            var testData = TestMethodData();
            var expectedCalendar = testData;
            var query = new GetCalendarByDaysQuery();

            _mockCalendarRepo.Setup(m => m.GetItemsAsync(It.IsAny<ISpecification<CalendarDay>>(), null, null))
                .ReturnsAsync(expectedCalendar);
            var sut = new GetCalendarByDaysHandler(_mockCalendarRepo.Object, _mapper);
            var act = await sut.HandleAsync(query);

            Assert.IsNotNull(act);
            Assert.IsInstanceOfType(act, typeof(IEnumerable<CalendarDto>));
        }

        private GetItemsResult<IEnumerable<CalendarDay>> TestMethodData()
        {
            var testData = new GetItemsResult<IEnumerable<CalendarDay>>();
            testData.ContinuationToken = null;

            testData.Results = new List<CalendarDay>()
            {
                new CalendarDay { Id = "4-5-4|2021-04-01", FiscalYear = 2021, CalendarType = "4-5-4", Quarter = 2, Period = 4, CalendarDate = "2021-04-01", WeekOfYear = 13, DayOfYear = 61, WeekOfPeriod = 1 },
                new CalendarDay { Id = "4-5-4|2021-08-01", FiscalYear = 2021, CalendarType = "4-5-4", Quarter = 3, Period = 8, CalendarDate = "2021-08-01", WeekOfYear = 24, DayOfYear = 121, WeekOfPeriod = 1 }, 
                new CalendarDay { Id = "4-5-4|2021-01-01", FiscalYear = 2021, CalendarType = "4-5-4", Quarter = 1, Period = 1, CalendarDate = "2021-01-01", WeekOfYear = 1, DayOfYear = 1, WeekOfPeriod = 1 }
            };
            return testData;
        }

As you can see, I am testing for not null and that it does contain the typeof CalendarDto. I would appreciate any asisstance as to how to validate that "act" is in orderby date. Thanks!

Source: Visual Studio Questions

LEAVE A COMMENT