Add extra unit tests and functional tests for CompositeViewEngine

Fixes #768
This commit is contained in:
Pranav K 2014-07-09 15:03:34 -07:00
parent 23eefed31a
commit 177aadeadf
13 changed files with 354 additions and 0 deletions

13
Mvc.sln
View File

@ -49,6 +49,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "RoutingWebSite", "test\WebS
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.Test", "test\Microsoft.AspNet.Mvc.Test\Microsoft.AspNet.Mvc.Test.kproj", "{5F945B82-FE5F-425C-956C-8BC2F2020254}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CompositeViewEngine", "test\WebSites\CompositeViewEngine\CompositeViewEngine.kproj", "{A853B2BA-4449-4908-A416-5A3C027FC22B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -249,6 +251,16 @@ Global
{5F945B82-FE5F-425C-956C-8BC2F2020254}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5F945B82-FE5F-425C-956C-8BC2F2020254}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5F945B82-FE5F-425C-956C-8BC2F2020254}.Release|x86.ActiveCfg = Release|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Debug|x86.ActiveCfg = Debug|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Release|Any CPU.Build.0 = Release|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{A853B2BA-4449-4908-A416-5A3C027FC22B}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -274,5 +286,6 @@ Global
{680D75ED-601F-4D86-B01B-1072D0C31B8C} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{42CDBF4A-E238-4C0F-A416-44588363EB4C} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{5F945B82-FE5F-425C-956C-8BC2F2020254} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
{A853B2BA-4449-4908-A416-5A3C027FC22B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
EndGlobalSection
EndGlobal

View File

@ -10,6 +10,67 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
public class CompositeViewEngineTest
{
[Fact]
public void FindView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered()
{
// Arrange
var viewName = "test-view";
var provider = new Mock<IViewEngineProvider>();
provider.SetupGet(p => p.ViewEngines)
.Returns(new IViewEngine[0]);
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindView(new Dictionary<string, object>(), viewName);
// Assert
Assert.False(result.Success);
Assert.Empty(result.SearchedLocations);
}
[Fact]
public void FindView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult()
{
// Arrange
var viewName = "test-view";
var provider = new Mock<IViewEngineProvider>();
var engine = new Mock<IViewEngine>();
engine.Setup(e => e.FindView(It.IsAny<IDictionary<string, object>>(), It.IsAny<string>()))
.Returns(ViewEngineResult.NotFound(viewName, new[] { "controller/test-view" }));
provider.SetupGet(p => p.ViewEngines)
.Returns(new[] { engine.Object });
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindView(new Dictionary<string, object>(), viewName);
// Assert
Assert.False(result.Success);
Assert.Equal(new[] { "controller/test-view" }, result.SearchedLocations);
}
[Fact]
public void FindView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturnsAFoundResult()
{
// Arrange
var viewName = "test-view";
var provider = new Mock<IViewEngineProvider>();
var engine = new Mock<IViewEngine>();
var view = Mock.Of<IView>();
engine.Setup(e => e.FindView(It.IsAny<IDictionary<string, object>>(), It.IsAny<string>()))
.Returns(ViewEngineResult.Found(viewName, view));
provider.SetupGet(p => p.ViewEngines)
.Returns(new[] { engine.Object });
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindView(new Dictionary<string, object>(), viewName);
// Assert
Assert.True(result.Success);
Assert.Same(view, result.View);
}
[Fact]
public void FindView_ReturnsViewFromFirstViewEngineWithFoundResult()
{
@ -69,6 +130,67 @@ namespace Microsoft.AspNet.Mvc.Rendering
Assert.Equal(new[] { "1", "2", "3", "4", "5" }, result.SearchedLocations);
}
[Fact]
public void FindPartialView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered()
{
// Arrange
var viewName = "my-partial-view";
var provider = new Mock<IViewEngineProvider>();
provider.SetupGet(p => p.ViewEngines)
.Returns(new IViewEngine[0]);
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindPartialView(new Dictionary<string, object>(), viewName);
// Assert
Assert.False(result.Success);
Assert.Empty(result.SearchedLocations);
}
[Fact]
public void FindPartialView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult()
{
// Arrange
var viewName = "partial-view";
var provider = new Mock<IViewEngineProvider>();
var engine = new Mock<IViewEngine>();
engine.Setup(e => e.FindPartialView(It.IsAny<IDictionary<string, object>>(), It.IsAny<string>()))
.Returns(ViewEngineResult.NotFound(viewName, new[] { "shared/partial-view" }));
provider.SetupGet(p => p.ViewEngines)
.Returns(new[] { engine.Object });
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindPartialView(new Dictionary<string, object>(), viewName);
// Assert
Assert.False(result.Success);
Assert.Equal(new[] { "shared/partial-view" }, result.SearchedLocations);
}
[Fact]
public void FindPartialView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturnsAFoundResult()
{
// Arrange
var viewName = "test-view";
var provider = new Mock<IViewEngineProvider>();
var engine = new Mock<IViewEngine>();
var view = Mock.Of<IView>();
engine.Setup(e => e.FindPartialView(It.IsAny<IDictionary<string, object>>(), It.IsAny<string>()))
.Returns(ViewEngineResult.Found(viewName, view));
provider.SetupGet(p => p.ViewEngines)
.Returns(new[] { engine.Object });
var compositeViewEngine = new CompositeViewEngine(provider.Object);
// Act
var result = compositeViewEngine.FindPartialView(new Dictionary<string, object>(), viewName);
// Assert
Assert.True(result.Success);
Assert.Same(view, result.View);
}
[Fact]
public void FindPartialView_ReturnsViewFromFirstViewEngineWithFoundResult()
{

View File

@ -0,0 +1,52 @@
// 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.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost;
using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
public class CompositeViewEngineTests
{
private readonly IServiceProvider _services;
private readonly Action<IBuilder> _app = new CompositeViewEngine.Startup().Configure;
public CompositeViewEngineTests()
{
_services = TestHelper.CreateServices("CompositeViewEngine");
}
[Fact]
public async Task CompositeViewEngine_FindsPartialViewsAcrossAllEngines()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.Handler;
// Act
var response = await client.GetAsync("http://localhost/");
// Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("Hello world", body.Trim());
}
[Fact]
public async Task CompositeViewEngine_FindsViewsAcrossAllEngines()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.Handler;
// Act
var response = await client.GetAsync("http://localhost/Home/TestView");
// Assert
var body = await response.ReadBodyAsStringAsync();
Assert.Equal("Content from test view", body.Trim());
}
}
}

View File

@ -31,6 +31,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ActivatorTests.cs" />
<Compile Include="CompositeViewEngineTests.cs" />
<Compile Include="DependencyResolverTests.cs" />
<Compile Include="InlineConstraintTests.cs" />
<Compile Include="RoutingTests.cs" />

View File

@ -5,6 +5,7 @@
"dependencies": {
"ActivatorWebSite": "",
"BasicWebSite": "",
"CompositeViewEngine": "",
"InlineConstraintsWebSite": "",
"Microsoft.AspNet.TestHost": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": "",

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>a853b2ba-4449-4908-a416-5a3c027fc22b</ProjectGuid>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition="$(OutputType) == 'Console'">
<DebuggerFlavor>ConsoleDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="$(OutputType) == 'Web'">
<DebuggerFlavor>WebDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Content Include="Project.json" />
<Content Include="Views\Home\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HomeController.cs" />
<Compile Include="Startup.cs" />
<Compile Include="TestPartialView.cs" />
<Compile Include="TestView.cs" />
<Compile Include="TestViewEngine.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,20 @@
// 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 Microsoft.AspNet.Mvc;
namespace CompositeViewEngine
{
public class HomeController
{
public ViewResult Index()
{
return new ViewResult();
}
public ViewResult TestView()
{
return new ViewResult { ViewName = "test-view" };
}
}
}

View File

@ -0,0 +1,31 @@
// 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 Microsoft.AspNet.Builder;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.DependencyInjection;
namespace CompositeViewEngine
{
public class Startup
{
public void Configure(IBuilder app)
{
var configuration = app.GetTestConfiguration();
// Set up application services
app.UseServices(services =>
{
// Add a view engine as the first one in the list.
services.AddMvc(configuration)
.SetupOptions<MvcOptions>(options =>
{
options.ViewEngines.Insert(0, typeof(TestViewEngine));
});
});
// Add MVC to the request pipeline
app.UseMvc();
}
}
}

View File

@ -0,0 +1,17 @@
// 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.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
namespace CompositeViewEngine
{
public class TestPartialView : IView
{
public async Task RenderAsync(ViewContext context)
{
await context.Writer.WriteLineAsync("world");
}
}
}

View File

@ -0,0 +1,17 @@
// 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.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
namespace CompositeViewEngine
{
public class TestView : IView
{
public async Task RenderAsync(ViewContext context)
{
await context.Writer.WriteLineAsync("Content from test view");
}
}
}

View File

@ -0,0 +1,30 @@
// 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.Collections.Generic;
using Microsoft.AspNet.Mvc.Rendering;
namespace CompositeViewEngine
{
public class TestViewEngine : IViewEngine
{
public ViewEngineResult FindPartialView(IDictionary<string, object> context, string partialViewName)
{
if (string.Equals(partialViewName, "partial-test-view", StringComparison.Ordinal))
{
return ViewEngineResult.Found(partialViewName, new TestPartialView());
}
return ViewEngineResult.NotFound(partialViewName, new[] { partialViewName });
}
public ViewEngineResult FindView(IDictionary<string, object> context, string viewName)
{
if (string.Equals(viewName, "test-view"))
{
return ViewEngineResult.Found(viewName, new TestView());
}
return ViewEngineResult.NotFound(viewName, new[] { viewName });
}
}
}

View File

@ -0,0 +1 @@
Hello @await Html.PartialAsync("partial-test-view")

View File

@ -0,0 +1,12 @@
{
"dependencies": {
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Mvc.TestConfiguration": "",
"Microsoft.Framework.ConfigurationModel": ""
},
"configurations": {
"net45": { },
"k10": { }
}
}