diff --git a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs index ee5d5f7fe5..bab0de1d5a 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs @@ -123,7 +123,7 @@ namespace Microsoft.AspNet.Mvc.Razor } /// - /// Section '{0}' is not defined + /// Section '{0}' is not defined. /// internal static string SectionNotDefined { @@ -131,7 +131,7 @@ namespace Microsoft.AspNet.Mvc.Razor } /// - /// Section '{0}' is not defined + /// Section '{0}' is not defined. /// internal static string FormatSectionNotDefined(object p0) { diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 2bb0bd077c..d2a9c692ff 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Mvc.Razor { public abstract class RazorView : IView { - public IViewComponentHelper Component + public IViewComponentHelper Component { get { return Context == null ? null : Context.Component; } } @@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc.Razor protected TextWriter Output { get; set; } - public IUrlHelper Url + public IUrlHelper Url { get { return Context == null ? null : Context.Url; } } @@ -95,15 +95,6 @@ namespace Microsoft.AspNet.Mvc.Razor public abstract Task ExecuteAsync(); - public void DefineSection(string name, HelperResult action) - { - if (SectionWriters.ContainsKey(name)) - { - throw new InvalidOperationException(Resources.FormatSectionAlreadyDefined(name)); - } - SectionWriters[name] = action; - } - public virtual void Write(object value) { WriteTo(Output, value); @@ -259,15 +250,25 @@ namespace Microsoft.AspNet.Mvc.Razor return new HtmlString(BodyContent); } - public HelperResult RenderSection(string name) + public void DefineSection(string name, HelperResult action) { - return RenderSection(name, required: false); + if (SectionWriters.ContainsKey(name)) + { + throw new InvalidOperationException(Resources.FormatSectionAlreadyDefined(name)); + } + SectionWriters[name] = action; } - public HelperResult RenderSection(string name, bool required) + public HelperResult RenderSection([NotNull] string name) + { + return RenderSection(name, required: true); + } + + public HelperResult RenderSection([NotNull] string name, bool required) { HelperResult action; - if (PreviousSectionWriters.TryGetValue(name, out action)) + if (PreviousSectionWriters != null && + PreviousSectionWriters.TryGetValue(name, out action)) { return action; } @@ -283,4 +284,4 @@ namespace Microsoft.AspNet.Mvc.Razor } } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx index 684beb7909..1d38783d05 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx @@ -139,7 +139,7 @@ Section '{0}' is already defined. - Section '{0}' is not defined + Section '{0}' is not defined. The partial view '{0}' was not found. The following locations were searched:{1} diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs new file mode 100644 index 0000000000..fcc9100950 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs @@ -0,0 +1,111 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc.Rendering; +using Moq; +using Xunit; + +namespace Microsoft.AspNet.Mvc.Razor.Test +{ + public class RazorViewTest + { + private const string LayoutPath = "~/Shared/_Layout.cshtml"; + + [Fact] + public async Task DefineSection_ThrowsIfSectionIsAlreadyDefined() + { + // Arrange + Exception ex = null; + var view = CreateView(v => + { + v.DefineSection("foo", new HelperResult(action: null)); + + ex = Assert.Throws( + () => v.DefineSection("foo", new HelperResult(action: null))); + }); + var viewContext = CreateViewContext(layoutView: null); + + // Act + await view.RenderAsync(viewContext); + + // Assert + Assert.Equal("Section 'foo' is already defined.", ex.Message); + } + + [Fact] + public async Task RenderSection_RendersSectionFromPreviousPage() + { + // Arrange + var expected = new HelperResult(action: null); + HelperResult actual = null; + var view = CreateView(v => + { + v.DefineSection("bar", expected); + v.Layout = LayoutPath; + }); + var layoutView = CreateView(v => + { + actual = v.RenderSection("bar"); + }); + var viewContext = CreateViewContext(layoutView); + + // Act + await view.RenderAsync(viewContext); + + // Assert + Assert.Same(actual, expected); + } + + [Fact] + public async Task RenderSection_ThrowsIfRequiredSectionIsNotFound() + { + // Arrange + var expected = new HelperResult(action: null); + Exception ex = null; + var view = CreateView(v => + { + v.DefineSection("baz", expected); + v.Layout = LayoutPath; + }); + var layoutView = CreateView(v => + { + ex = Assert.Throws( + () => v.RenderSection("bar")); + }); + var viewContext = CreateViewContext(layoutView); + + // Act + await view.RenderAsync(viewContext); + + // Assert + Assert.Equal("Section 'bar' is not defined.", ex.Message); + } + + public static RazorView CreateView(Action executeAction) + { + var view = new Mock { CallBase = true }; + if (executeAction != null) + { + view.Setup(v => v.ExecuteAsync()) + .Callback(() => executeAction(view.Object)) + .Returns(Task.FromResult(0)); + } + + return view.Object; + } + + private static ViewContext CreateViewContext(IView layoutView) + { + var viewFactory = new Mock(); + viewFactory.Setup(v => v.CreateInstance(LayoutPath)) + .Returns(layoutView); + var serviceProvider = new Mock(); + serviceProvider.Setup(f => f.GetService(typeof(IVirtualPathViewFactory))) + .Returns(viewFactory.Object); + return new ViewContext(serviceProvider.Object, httpContext: null, viewEngineContext: null) + { + Writer = new StringWriter() + }; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/project.json b/test/Microsoft.AspNet.Mvc.Razor.Test/project.json index 938b0e2de5..0704abb6c3 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/project.json +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/project.json @@ -1,9 +1,11 @@ { "version" : "0.1-alpha-*", "dependencies": { + "Microsoft.AspNet.Abstractions": "0.1-alpha-*", "Microsoft.AspNet.FileSystems": "0.1-alpha-*", "Microsoft.AspNet.Razor": "0.1-alpha-*", "Microsoft.AspNet.Mvc.Razor" : "", + "Microsoft.AspNet.Mvc.Rendering" : "", "Microsoft.AspNet.Testing" : "0.1-alpha-*", "Xunit.KRunner": "0.1-alpha-*", "xunit.abstractions": "2.0.0-aspnet-*",