public void GetChapterSummaryShouldReturnContentsIfParamatersAreValid() { //Arrange var existingChapter = new ChapterBuilder().WithId().Build(); var chapterContents = new ChapterSummaryModel(); var userExerciseResults = new List <AssignmentResultDto>(); _chapterServiceMock.Setup(service => service.LoadChapterWithTestsAsync(It.IsAny <int>(), It.IsAny <int>())) .ReturnsAsync(existingChapter); _chapterServiceMock.Setup(service => service.GetResultsForUserAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime?>())) .ReturnsAsync(userExerciseResults); _chapterConverterMock.Setup(converter => converter.ToChapterSummaryModel(It.IsAny <Chapter>(), It.IsAny <IList <AssignmentResultDto> >())) .Returns(chapterContents); //Act var actionResult = _controller.GetChapterSummary(existingChapter.CourseId, existingChapter.Number, _userId, null).Result as OkObjectResult; //Assert Assert.That(actionResult, Is.Not.Null); _chapterServiceMock.Verify(service => service.LoadChapterWithTestsAsync(existingChapter.CourseId, existingChapter.Number), Times.Once); _chapterServiceMock.Verify(service => service.GetResultsForUserAsync(existingChapter.Id, _userId, null), Times.Once); _chapterConverterMock.Verify(converter => converter.ToChapterSummaryModel(existingChapter, userExerciseResults), Times.Once); Assert.That(actionResult.Value, Is.EqualTo(chapterContents)); }
public ChapterSummaryModel ToChapterSummaryModel(Chapter chapter, IList <AssignmentResultDto> userExerciseResults) { if (chapter.Exercises == null) { throw new ArgumentException("Chapter should have exercises loaded", nameof(chapter)); } if (chapter.Exercises.Any(ex => ex.Tests == null)) { throw new ArgumentException("All exercises of the chapter should have their tests loaded", nameof(chapter)); } var model = new ChapterSummaryModel { Id = chapter.Id, Number = chapter.Number, ExerciseSummaries = new List <ExerciseSummaryModel>() }; foreach (var exercise in chapter.Exercises.OrderBy(exercise => exercise.Code)) { var userExerciseSummaryModel = CreateExerciseSummaryModel(exercise, userExerciseResults); model.ExerciseSummaries.Add(userExerciseSummaryModel); } return(model); }