From 26f98b481ab1aa2d44d4185944ec37b45c394143 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 13 Aug 2014 17:33:06 -0700 Subject: [PATCH] ViewComponents should render partial views Fixes #960 --- .../ViewComponents/ViewViewComponentResult.cs | 3 +- .../ViewViewComponentResultTest.cs | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/ViewViewComponentResultTest.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs index cd1a7b7883..5cb32e5ec6 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Globalization; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Core; @@ -77,7 +76,7 @@ namespace Microsoft.AspNet.Mvc private IView FindView(ActionContext context, string viewName) { - var result = _viewEngine.FindView(context, viewName); + var result = _viewEngine.FindPartialView(context, viewName); if (!result.Success) { var locations = string.Empty; diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/ViewViewComponentResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/ViewViewComponentResultTest.cs new file mode 100644 index 0000000000..a3da98501a --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/ViewViewComponentResultTest.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc.ModelBinding; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.PipelineCore; +using Microsoft.AspNet.Routing; +using Moq; +using Xunit; + +namespace Microsoft.AspNet.Mvc +{ + public class ViewViewComponentResultTest + { + [Fact] + public async Task ExecuteAsync_RendersPartialViews() + { + // Arrange + var view = Mock.Of(); + var viewEngine = new Mock(MockBehavior.Strict); + viewEngine.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) + .Returns(ViewEngineResult.Found("some-view", view)) + .Verifiable(); + var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); + var result = new ViewViewComponentResult(viewEngine.Object, "some-view", viewData); + var viewComponentContext = GetViewComponentContext(view, viewData); + + // Act + await result.ExecuteAsync(viewComponentContext); + + // Assert + viewEngine.Verify(); + } + + [Fact] + public async Task ExecuteAsync_ThrowsIfPartialViewCannotBeFound() + { + // Arrange + var expected = +@"The view 'Components/Object/some-view' was not found. The following locations were searched: +foo +bar."; + var view = Mock.Of(); + var viewEngine = new Mock(MockBehavior.Strict); + viewEngine.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny())) + .Returns(ViewEngineResult.NotFound("some-view", new[] { "foo", "bar" })) + .Verifiable(); + var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); + var result = new ViewViewComponentResult(viewEngine.Object, "some-view", viewData); + var viewComponentContext = GetViewComponentContext(view, viewData); + + // Act and Assert + var ex = await Assert.ThrowsAsync( + async () => await result.ExecuteAsync(viewComponentContext)); + Assert.Equal(expected, ex.Message); + } + + private static ViewComponentContext GetViewComponentContext(IView view, ViewDataDictionary viewData) + { + var actionContext = new ActionContext(new RouteContext(new DefaultHttpContext()), new ActionDescriptor()); + var viewContext = new ViewContext(actionContext, view, viewData, TextWriter.Null); + var viewComponentContext = new ViewComponentContext(typeof(object).GetTypeInfo(), viewContext, TextWriter.Null); + return viewComponentContext; + } + } +} \ No newline at end of file