private void AssertPublicationDetailsViewModel(PublicationDetailsViewModel expected, PublicationDetailsViewModel actual) { this.AssertPublicationDetailsServiceModel(expected.Publication, actual.Publication); this.AssertComment(expected.Comments.First(), actual.Comments.First()); this.AssertComment(expected.Comments.Last(), actual.Comments.Last()); }
public IActionResult Details(int id, int page) { if (page <= 0) { page = 1; } int?userId = null; if (User.Identity.IsAuthenticated) { userId = int.Parse(this.userManager.GetUserId(User)); } int totalComments = this.commentService.Total(id); PublicationDetailsViewModel model = new PublicationDetailsViewModel { CurrentPage = page, TotalEntries = totalComments, EntriesPerPage = CommentsPerPage, Publication = this.publicationService.Details(id), Comments = this.commentService.All(id, page, CommentsPerPage, userId) }; if (model.Publication == null) { return(BadRequest()); } return(View(model)); }
public void Details_WithValidId_ShouldReturnView() { // Arrange Mock <ICommentService> commentService = new Mock <ICommentService>(); Mock <IPublicationService> publicationService = new Mock <IPublicationService>(); PublicationDetailsServiceModel detailsModel = this.GetPublicationDetailsServiceModel(); const int publicationId = 1; commentService .Setup(c => c.Total(publicationId)) .Returns(20); commentService .Setup(c => c.All( It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int?>())) .Returns(this.GetComments()); publicationService .Setup(p => p.Details(It.IsAny <int>())) .Returns(detailsModel); Mock <ClaimsPrincipal> claimsMock = new Mock <ClaimsPrincipal>(); claimsMock.Setup(t => t.Identity.IsAuthenticated) .Returns(false); Mock <HttpContext> mockHttpContext = new Mock <HttpContext>(); mockHttpContext.Setup(m => m.User).Returns(claimsMock.Object); PublicationsController publicationsController = new PublicationsController(commentService.Object, null, null, publicationService.Object, null) { ControllerContext = new ControllerContext { HttpContext = mockHttpContext.Object } }; // Act IActionResult result = publicationsController.Details(publicationId, 2); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <PublicationDetailsViewModel>(model); PublicationDetailsViewModel returnModel = model as PublicationDetailsViewModel; this.AssertPublicationDetailsViewModel(this.GetPublicationDetailsViewModel(), returnModel); }