public void Execute_ResolvesView_WithDefaultAsViewName() { // Arrange var view = new Mock<IView>(MockBehavior.Strict); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine.Setup(e => e.FindPartialView(It.IsAny<ActionContext>(), It.IsAny<string>())) .Returns(ViewEngineResult.Found("Default", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); }
public void WhenHeadingH4Invoked_ThenViewModelIsUpdated(string key, string expected) { var component = new HeadingH4(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(new Dictionary <string, string>()) as ViewViewComponentResult; HeadingModel resultModel = (HeadingModel)result.ViewData.Model; //Assert expected.Should().Be(ViewComponentTestHelper.GetPropertyValue(resultModel, key)); }
public IViewComponentResult EditExercisePurpose(ExercisePurposeViewModel viewModel) { ViewViewComponentResult result = (ViewViewComponentResult)EditLearnerFeature(viewModel); if (result.ViewData.Model is not UserProfile) { return(result); } result.ViewName = "~/Views/LearnerProfile/Module/EditExercisePurpose.cshtml"; return(result); }
public void Invoke_GetsCorrectNumber() { IBlogPostRepository repo = Substitute.For <IBlogPostRepository>(); repo.Posts.Returns(GenerateFakePosts()); RecentPostsViewComponent comp = new RecentPostsViewComponent(repo); ViewViewComponentResult result = (ViewViewComponentResult)comp.Invoke(4); var model = (RecentPostsViewModel)result.ViewData.Model; Assert.AreEqual(model.Posts.Count(), 4); }
public IViewComponentResult Invoke() { var model = new PartialViewMacroModel( _content, _macro.Id, _macro.Alias, _macro.Name, _macro.Properties.ToDictionary(x => x.Key, x => (object)x.Value)); ViewViewComponentResult result = View(_macro.MacroSource, model); return(result); }
public IViewComponentResult CalendarEventItems(FullCalendarViewModel viewModel) { ViewViewComponentResult result = (ViewViewComponentResult)CalendarEvents(viewModel, true); if (viewModel.MasterVer == Naming.MasterVersion.Ver2020) { result.ViewName = "~/Views/ConsoleHome/Index/Coach/EventItems.cshtml"; } else { result.ViewName = "~/Views/ConsoleHome/Module/EventItems.cshtml"; } return(result); }
public void WhenRadioHintInvoked_TheViewModelIsUpdated(string key, string value) { var values = new Dictionary <string, string>() { }; var component = new RadioHint(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; HintModel resultModel = (HintModel)result.ViewData.Model; //Assert value.Should().Be(ViewComponentTestHelper.GetPropertyValue(resultModel, key)); }
public void Can_Return_Correct_View_For_Logged_User() { ClaimsPrincipal fakeClaimsPrincipal = new ClaimsPrincipal(); fakeClaimsPrincipal.AddIdentity(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "NotAdmin") })); Mock <HttpContext> mockHttpContext = new Mock <HttpContext>(); mockHttpContext.SetupGet(x => x.User).Returns(fakeClaimsPrincipal); SetFakeHttpContext(mockHttpContext); _mockSignInManager.Setup(m => m.IsSignedIn(fakeClaimsPrincipal)).Returns(true); _mockUserManager.Setup(m => m.GetUserAsync(fakeClaimsPrincipal)).ReturnsAsync((IdentityUser)null); ViewViewComponentResult result = (ViewViewComponentResult)_loginStatusViewComponent.InvokeAsync().Result; Assert.Equal("LoggedIn", result.ViewName); }
public void WhenBackLinkInvoked_ThenViewModelLinkTextShouldBeSetToBackAsDefault() { var values = new Dictionary <string, string>() { }; var component = new BackLink(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; LinkModel resultModel = (LinkModel)result.ViewData.Model; //Assert "Back".Should().Be(ViewComponentTestHelper.GetPropertyValue(resultModel, "LinkText")); }
public void Invoke_Without_Items_Should_Display_Empty_View() { //arrange var items = new List <BasketItem>(); CustomerBasket customerBasket = new CustomerBasket { Items = items }; var vc = new BasketListViewComponent(); //act var result = vc.Invoke(customerBasket, false); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("Empty", vvcResult.ViewName); }
public void WhenRadioLabelInvoked_TheViewModelIsUpdated(string key, string value) { var values = new Dictionary <string, string>() { { key, value } }; var component = new RadioLabel(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; LabelModel resultModel = (LabelModel)result.ViewData.Model; //Assert resultModel.AdditionalClass.Should().Contain(value); }
public void Invoke_PostsInMostRecentFirstOrder() { IBlogPostRepository repo = Substitute.For <IBlogPostRepository>(); repo.Posts.Returns(GenerateFakePosts()); RecentPostsViewComponent comp = new RecentPostsViewComponent(repo); ViewViewComponentResult result = (ViewViewComponentResult)comp.Invoke(4); var model = (RecentPostsViewModel)result.ViewData.Model; var posts = model.Posts.ToList(); for (int i = 0; i < posts.Count - 1; i++) { Assert.IsTrue(posts[i].DatePublished > posts[i + 1].DatePublished); } }
public void WhenCheckBoxInvoked_ThenViewModelIsUpdated(string key, string value) { var values = new Dictionary <string, string>() { { key, value } }; var component = new Checkbox(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; CheckBoxModel resultModel = (CheckBoxModel)result.ViewData.Model; //Assert Assert.AreEqual(value, ViewComponentTestHelper.GetPropertyValue(resultModel, key)); }
public void WhenPrimaryHeroBannerInvoked_ThenViewModelIsUpdated(string key, string value) { var values = new Dictionary <string, string>() { { key, value } }; var component = new PrimaryHeroBanner(); component.ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(); ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; BannerModel resultModel = (BannerModel)result.ViewData.Model; //Assert value.Should().Be(ViewComponentTestHelper.GetPropertyValue(resultModel, key)); }
public void Invoke_Without_Items_Should_Display_Empty_View() { //arrange Mock <IBasketService> basketServiceMock = new Mock <IBasketService>(); basketServiceMock.Setup(m => m.GetBasketItems()) .Returns(new List <BasketItem>()); var vc = new BasketListViewComponent(basketServiceMock.Object); //act var result = vc.Invoke(false); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("Empty", vvcResult.ViewName); }
public void WhenStartButtonLinkInvoked_ThenViewModelIsUpdated(string key, string value) { var values = new Dictionary <string, string>() { { key, value } }; var component = new StartButtonLink { ViewComponentContext = ViewComponentTestHelper.GetViewComponentContext(), }; ViewViewComponentResult result = component.Invoke(values) as ViewViewComponentResult; LinkModel resultModel = (LinkModel)result.ViewData.Model; //Assert value.Should().Be(ViewComponentTestHelper.GetPropertyValue(resultModel, key)); }
public void SelectsCorrectLocationsAsActive() { // Arrange NavBar navBar = new NavBar { ViewComponentContext = new ViewComponentContext { ViewContext = new ViewContext { RouteData = new RouteData { Values = { ["Controller"] = "Home", ["Action"] = "Index" } } } } }; Location[] locations = { new Location("Home", "Index"), new Location("Users", "Index", "Regular Users Homepage"), new Location("Admins", "Index", "Admin Users Homepage") }; // Act ViewViewComponentResult view = navBar.Invoke(locations); // Assert object viewModel = view.ViewData.Model; Assert.IsAssignableFrom <IEnumerable <NavLocation> >(viewModel); NavLocation[] navLocations = ((IEnumerable <NavLocation>)view.ViewData.Model).ToArray(); Assert.Equal(3, navLocations.Length); Assert.True(navLocations[0].IsActive); Assert.False(navLocations[1].IsActive); Assert.False(navLocations[2].IsActive); }
public void Indicate_Selected_Category() { //Arrange Mock <IProductRepository> mockRepository = new Mock <IProductRepository>(); mockRepository.SetupGet(m => m.Products) .Returns((new[] { new Product { ProductID = 1, Category = "B" }, new Product { ProductID = 2, Category = "B" }, new Product { ProductID = 3, Category = "A" }, new Product { ProductID = 4, Category = "A" }, new Product { ProductID = 5, Category = "B" } } ).AsQueryable()); NavigationMenuViewComponent target = new NavigationMenuViewComponent(mockRepository.Object); target.ViewComponentContext = new ViewComponentContext() { ViewContext = new ViewContext() { RouteData = new RouteData() } }; target.RouteData.Values["categoryy"] = "B"; //Act ViewViewComponentResult componentResult = (ViewViewComponentResult)target.Invoke(); string result = (string)componentResult.ViewData["categoryToSelectt"]; //Assert Assert.Equal("B", result); }
public void Execute_ResolvesView_AndWritesDiagnosticListener() { // Arrange var view = new Mock <IView>(MockBehavior.Strict); view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/Default", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/Default", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, TempData = _tempDataDictionary, }; var adapter = new TestDiagnosticListener(); var viewComponentContext = GetViewComponentContext(view.Object, viewData, adapter); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.View); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.View); }
public async Task ExecuteAsync_ResolvesViewEngineFromServiceProvider_IfNoViewEngineIsExplicitlyProvided() { // Arrange var view = Mock.Of <IView>(); var viewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty <string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view)) .Verifiable(); var serviceProvider = new Mock <IServiceProvider>(); serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) .Returns(viewEngine.Object); serviceProvider.Setup(p => p.GetService(typeof(DiagnosticListener))) .Returns(new DiagnosticListener("Test")); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view, viewData); viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider.Object; // Act await result.ExecuteAsync(viewComponentContext); // Assert viewEngine.Verify(); serviceProvider.Verify(); }
public async Task ExecuteAsync_ThrowsIfPartialViewCannotBeFound() { // Arrange var expected = string.Join(Environment.NewLine, "The view 'Components/Invoke/some-view' was not found. The following locations were searched:", "view-location1", "view-location2", "view-location3", "view-location4"); var view = Mock.Of <IView>(); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", new[] { "view-location1", "view-location2" })) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound( "Components/Invoke/some-view", new[] { "view-location3", "view-location4" })) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view, viewData); // Act and Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>( () => result.ExecuteAsync(viewComponentContext)); Assert.Equal(expected, ex.Message); }
public void Invoke_Should_Display_Default_View() { //arrange var vc = new BasketItemViewComponent(); BasketItem item = new BasketItem { Id = 1, ProductId = 1, Name = "Broccoli", UnitPrice = 59.90m, Quantity = 2 }; //act var result = vc.Invoke(item); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("Default", vvcResult.ViewName); BasketItem resultModel = Assert.IsAssignableFrom <BasketItem>(vvcResult.ViewData.Model); Assert.Equal(item.ProductId, resultModel.ProductId); }
public void Invoke_Should_Display_SummaryItem_View() { //arrange var vc = new BasketItemViewComponent(); BasketItem item = new BasketItem { Id = 2, ProductId = 5, Name = "Green Grapes", UnitPrice = 59.90m, Quantity = 3 }; //act var result = vc.Invoke(item, true); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("SummaryItem", vvcResult.ViewName); BasketItem resultModel = Assert.IsAssignableFrom <BasketItem>(vvcResult.ViewData.Model); Assert.Equal(item.ProductId, resultModel.ProductId); }
public void Execute_DoesNotWrapThrownExceptionsInAggregateExceptions() { // Arrange var expected = new IndexOutOfRangeException(); var view = new Mock <IView>(); view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>())) .Throws(expected) .Verifiable(); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty <string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act var actual = Record.Exception(() => result.Execute(viewComponentContext)); // Assert Assert.Same(expected, actual); view.Verify(); }
public async Task <IViewComponentResult> InvokeAsync() { ViewViewComponentResult viewComponent = null; bool isRegistered = _widgetProvider.IsRegistered(WidgetZone.TransactionReference); if (isRegistered) { string transactionRef = await LogTransactionReference(); viewComponent = await Task.FromResult(View(new ConfigurationModel() { TransactionReference = transactionRef })); } else { viewComponent = await Task.FromResult(View(new ConfigurationModel())); } return(viewComponent); }
public void Invoke_With_Items_Should_Display_Default_View() { //arrange Mock <IBasketService> basketServiceMock = new Mock <IBasketService>(); List <BasketItem> items = new List <BasketItem> { new BasketItem { Id = 1, ProductId = 1, Name = "Broccoli", UnitPrice = 59.90m, Quantity = 2 }, new BasketItem { Id = 2, ProductId = 5, Name = "Green Grapes", UnitPrice = 59.90m, Quantity = 3 }, new BasketItem { Id = 3, ProductId = 9, Name = "Tomato", UnitPrice = 59.90m, Quantity = 4 } }; basketServiceMock.Setup(m => m.GetBasketItems()) .Returns(items); var vc = new BasketListViewComponent(basketServiceMock.Object); //act var result = vc.Invoke(false); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("Default", vvcResult.ViewName); var model = Assert.IsAssignableFrom <BasketItemList>(vvcResult.ViewData.Model); Assert.Collection <BasketItem>(model.List, i => Assert.Equal(1, i.ProductId), i => Assert.Equal(5, i.ProductId), i => Assert.Equal(9, i.ProductId) ); }
public void Execute_RendersPartialViews() { // Arrange var view = new Mock <IView>(); view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty <string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); }
public void Execute_CallsFindView_WithExpectedPath_WhenViewNameIsSpecified(string viewName) { // Arrange var viewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.Found(viewName, new Mock <IView>().Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var componentContext = GetViewComponentContext(new Mock <IView>().Object, viewData); var componentResult = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, ViewName = viewName, TempData = _tempDataDictionary, }; // Act & Assert componentResult.Execute(componentContext); viewEngine.Verify(); }
public void Execute_RendersPartialViews() { // Arrange var view = new Mock<IView>(); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty<string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); }
public void Invoke_With_Items_Should_Display_Default_View() { //arrange List <BasketItem> items = new List <BasketItem> { new BasketItem { Id = "1", ProductId = "1", ProductName = "Broccoli", UnitPrice = 59.90m, Quantity = 2 }, new BasketItem { Id = "2", ProductId = "5", ProductName = "Green Grapes", UnitPrice = 59.90m, Quantity = 3 }, new BasketItem { Id = "3", ProductId = "9", ProductName = "Tomato", UnitPrice = 59.90m, Quantity = 4 } }; CustomerBasket customerBasket = new CustomerBasket { Items = items }; var vc = new BasketListViewComponent(); //act var result = vc.Invoke(customerBasket, false); //assert ViewViewComponentResult vvcResult = Assert.IsAssignableFrom <ViewViewComponentResult>(result); Assert.Equal("Default", vvcResult.ViewName); var model = Assert.IsAssignableFrom <BasketItemList>(vvcResult.ViewData.Model); Assert.Collection <BasketItem>(model.List, i => Assert.Equal("1", i.ProductId), i => Assert.Equal("5", i.ProductId), i => Assert.Equal("9", i.ProductId) ); }
public void Indicates_Selected_Category() { Mock <IGameRepository> mock = new Mock <IGameRepository>(); mock.Setup(m => m.Games).Returns(new Game[] { new Game { GameId = 1, Name = "Игра1", Category = "Симулятор" }, new Game { GameId = 2, Name = "Игра1", Category = "Шутер" } }); GameController controller = new GameController(mock.Object); controller.pageSize = 3; string category = "Шутер"; NavViewComponent target = new NavViewComponent(mock.Object); target.ViewData.Model = (GamesListViewModel)controller.List("Шутер", 1).ViewData.Model; ViewViewComponentResult r = (ViewViewComponentResult)target.Invoke(); string result = (string)target.ViewBag.SelectedCategory; Assert.Equal(category, result); }
public async Task ExecuteAsync_ResolvesViewEngineFromServiceProvider_IfNoViewEngineIsExplicitlyProvided() { // Arrange var view = Mock.Of<IView>(); var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty<string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view)) .Verifiable(); var serviceProvider = new Mock<IServiceProvider>(); serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) .Returns(viewEngine.Object); serviceProvider.Setup(p => p.GetService(typeof(DiagnosticSource))) .Returns(new DiagnosticListener("Test")); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view, viewData); viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider.Object; // Act await result.ExecuteAsync(viewComponentContext); // Assert viewEngine.Verify(); serviceProvider.Verify(); }
public void Execute_ResolvesView_AndWritesDiagnosticSource() { // Arrange var view = new Mock<IView>(MockBehavior.Strict); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/Default", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/Default", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, TempData = _tempDataDictionary, }; var adapter = new TestDiagnosticListener(); var viewComponentContext = GetViewComponentContext(view.Object, viewData, adapter); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.View); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.View); }
public void Execute_DoesNotWrapThrownExceptionsInAggregateExceptions() { // Arrange var expected = new IndexOutOfRangeException(); var view = new Mock<IView>(); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Throws(expected) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", Enumerable.Empty<string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/some-view", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act var actual = Record.Exception(() => result.Execute(viewComponentContext)); // Assert Assert.Same(expected, actual); view.Verify(); }
public async Task ExecuteAsync_ThrowsIfPartialViewCannotBeFound() { // Arrange var expected = string.Join(Environment.NewLine, "The view 'Components/Invoke/some-view' was not found. The following locations were searched:", "view-location1", "view-location2", "view-location3", "view-location4"); var view = Mock.Of<IView>(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, "some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound("some-view", new[] { "view-location1", "view-location2" })) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/some-view", /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound( "Components/Invoke/some-view", new[] { "view-location3", "view-location4" })) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData, TempData = _tempDataDictionary, }; var viewComponentContext = GetViewComponentContext(view, viewData); // Act and Assert var ex = await Assert.ThrowsAsync<InvalidOperationException>( () => result.ExecuteAsync(viewComponentContext)); Assert.Equal(expected, ex.Message); }
public void Execute_CallsFindView_WithExpectedPath_WhenViewNameIsSpecified(string viewName) { // Arrange var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.Found(viewName, new Mock<IView>().Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var componentContext = GetViewComponentContext(new Mock<IView>().Object, viewData); var componentResult = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, ViewName = viewName, TempData = _tempDataDictionary, }; // Act & Assert componentResult.Execute(componentContext); viewEngine.Verify(); }
public void Execute_CallsFindView_WithExpectedPath_WhenViewNameIsNullOrEmpty(string viewName) { // Arrange var shortName = "SomeShortName"; var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var componentContext = GetViewComponentContext(new Mock<IView>().Object, viewData); componentContext.ViewComponentDescriptor.ShortName = shortName; var expectedViewName = $"Components/{shortName}/Default"; var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), expectedViewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.Found(expectedViewName, new Mock<IView>().Object)) .Verifiable(); var componentResult = new ViewViewComponentResult(); componentResult.ViewEngine = viewEngine.Object; componentResult.ViewData = viewData; componentResult.ViewName = viewName; componentResult.TempData = _tempDataDictionary; // Act & Assert componentResult.Execute(componentContext); viewEngine.Verify(); }
public ViewTestBuilder(ActionTestContext testContext) : base(testContext) { this.viewResult = testContext.MethodResultAs <ViewViewComponentResult>(); }
public void Execute_DoesNotWrapThrownExceptionsInAggregateExceptions() { // Arrange var expected = new IndexOutOfRangeException(); var view = new Mock<IView>(); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Throws(expected) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine.Setup(e => e.FindPartialView(It.IsAny<ActionContext>(), It.IsAny<string>())) .Returns(ViewEngineResult.Found("some-view", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view.Object, viewData); // Act var actual = Record.Exception(() => result.Execute(viewComponentContext)); // Assert Assert.Same(expected, actual); view.Verify(); }
public void Execute_ThrowsIfPartialViewCannotBeFound() { // Arrange var expected = string.Join(Environment.NewLine, "The view 'Components/Object/some-view' was not found. The following locations were searched:", "location1", "location2."); var view = Mock.Of<IView>(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine.Setup(e => e.FindPartialView(It.IsAny<ActionContext>(), It.IsAny<string>())) .Returns(ViewEngineResult.NotFound("Components/Object/some-view", new[] { "location1", "location2" })) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view, viewData); // Act and Assert var ex = Assert.Throws<InvalidOperationException>(() => result.Execute(viewComponentContext)); Assert.Equal(expected, ex.Message); }
public async Task ExecuteAsync_RendersPartialViews() { // Arrange var view = Mock.Of<IView>(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine.Setup(e => e.FindPartialView(It.IsAny<ActionContext>(), It.IsAny<string>())) .Returns(ViewEngineResult.Found("some-view", view)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewName = "some-view", ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view, viewData); // Act await result.ExecuteAsync(viewComponentContext); // Assert viewEngine.Verify(); }
public void Execute_CallsFindPartialView_WithExpectedPath() { // Arrange var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindPartialView(It.IsAny<ActionContext>(), It.Is<string>(view => view.Contains("Components")))) .Returns(ViewEngineResult.Found(string.Empty, new Mock<IView>().Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var componentContext = GetViewComponentContext(new Mock<IView>().Object, viewData); var componentResult = new ViewViewComponentResult(); componentResult.ViewEngine = viewEngine.Object; componentResult.ViewData = viewData; // Act & Assert componentResult.Execute(componentContext); viewEngine.Verify(); }
public async Task ExecuteAsync_Throws_IfNoViewEngineCanBeResolved() { // Arrange var expected = "No service for type 'Microsoft.AspNet.Mvc.Rendering.ICompositeViewEngine'" + " has been registered."; var view = Mock.Of<IView>(); var serviceProvider = new ServiceCollection().BuildServiceProvider(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewName = "some-view", ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view, viewData); viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider; // Act and Assert var ex = await Assert.ThrowsAsync<InvalidOperationException>( () => result.ExecuteAsync(viewComponentContext)); Assert.Equal(expected, ex.Message); }
public async Task ExecuteAsync_ResolvesViewEngineFromServiceProvider_IfNoViewEngineIsExplicitlyProvided() { // Arrange var view = Mock.Of<IView>(); var viewEngine = new Mock<ICompositeViewEngine>(MockBehavior.Strict); viewEngine.Setup(e => e.FindPartialView(It.IsAny<ActionContext>(), It.IsAny<string>())) .Returns(ViewEngineResult.Found("some-view", view)) .Verifiable(); var serviceProvider = new Mock<IServiceProvider>(); serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine))) .Returns(viewEngine.Object); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewName = "some-view", ViewData = viewData }; var viewComponentContext = GetViewComponentContext(view, viewData); viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider.Object; // Act await result.ExecuteAsync(viewComponentContext); // Assert viewEngine.Verify(); serviceProvider.Verify(); }