public void Execute_WritesData_Encoded() { // Arrange var buffer = new MemoryStream(); var result = new ContentViewComponentResult("<Test />"); var viewComponentContext = GetViewComponentContext(Mock.Of <IView>(), buffer); // Act result.Execute(viewComponentContext); buffer.Position = 0; // Assert Assert.Equal("HtmlEncode[[<Test />]]", new StreamReader(buffer).ReadToEnd()); }
public void Execute_WritesData_Encoded() { // Arrange var buffer = new MemoryStream(); var result = new ContentViewComponentResult("<Test />"); var viewComponentContext = GetViewComponentContext(Mock.Of<IView>(), buffer); // Act result.Execute(viewComponentContext); buffer.Position = 0; // Assert Assert.Equal("HtmlEncode[[<Test />]]", new StreamReader(buffer).ReadToEnd()); }
public void Invoke_ContentViewComponentResult() { // Arrange int notificationsCount = 5, userId = 1; Mock <GenericRepository <Notification> > mockNotificationRepository = new Mock <GenericRepository <Notification> >(); mockNotificationRepository .Setup(nr => nr.Count(It.IsAny <Expression <Func <Notification, bool> > >())) .Returns(notificationsCount); Mock <IUnitOfWork> mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork .Setup(u => u.GetRepository <Notification, GenericRepository <Notification> >()) .Returns(mockNotificationRepository.Object); NotificationsCountViewComponent viewComponent = new NotificationsCountViewComponent(mockUnitOfWork.Object); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(p => p.FindFirst(It.IsAny <string>())) .Returns(new Claim(nameof(User.Id), userId.ToString())); viewComponent.ViewComponentContext = new ViewComponentContext { ViewContext = new Microsoft.AspNetCore.Mvc.Rendering.ViewContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act IViewComponentResult result = viewComponent.Invoke(); // Assert Assert.NotNull(result); ContentViewComponentResult contentViewComponentResult = Assert.IsType <ContentViewComponentResult>(result); Assert.NotNull(contentViewComponentResult.Content); Assert.Equal(notificationsCount.ToString(), contentViewComponentResult.Content); }