Add Autofac functional test.

Added very simple Autofac functional test that validates that we can add custom classes to the DI system as well as just use the ones provided to us.

#669
This commit is contained in:
N. Taylor Mullen 2014-06-16 16:51:39 -07:00
parent 23740085f1
commit 6cc47cd5f1
12 changed files with 187 additions and 1 deletions

15
Mvc.sln
View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.21806.0
VisualStudioVersion = 14.0.21722.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
EndProject
@ -41,6 +41,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ActivatorWebSite", "test\We
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "InlineConstraintsWebSite", "test\WebSites\InlineConstraintsWebSite\InlineConstraintsWebSite.kproj", "{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AutofacWebSite", "test\WebSites\AutofacWebSite\AutofacWebSite.kproj", "{07C0E921-FCBB-458C-AC11-3D01CE68B16B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -201,6 +203,16 @@ Global
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE}.Release|x86.ActiveCfg = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Debug|x86.ActiveCfg = Debug|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Any CPU.Build.0 = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{07C0E921-FCBB-458C-AC11-3D01CE68B16B}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -222,5 +234,6 @@ Global
{34DF1487-12C6-476C-BE0A-F31DF1939AE5} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{DB79BCBA-9538-4A53-87D9-77728E2BAA39} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{07C0E921-FCBB-458C-AC11-3D01CE68B16B} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Threading.Tasks;
using AutofacWebSite;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.TestHost;
using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests
{
public class DependencyResolverTests
{
[Theory]
[InlineData("http://localhost/di", "<p>Builder Output: Hello from builder.</p>")]
[InlineData("http://localhost/basic", "<p>Hello From Basic View</p>")]
public async Task AutofacDIContainerCanUseMvc(string url, string expectedResponseBody)
{
// Arrange
var provider = TestHelper.CreateServices("AutofacWebSite");
Action<IBuilder> app = new Startup().Configure;
TestServer server = null;
HttpResponse response = null;
// Act & Assert
await Assert.DoesNotThrowAsync(async () =>
{
// This essentially calls into the Startup.Configuration method
server = TestServer.Create(provider, app);
// Make a request to start resolving DI pieces
response = await server.Handler.GetAsync(url);
});
var actualResponseBody = new StreamReader(response.Body).ReadToEnd();
Assert.Equal(expectedResponseBody, actualResponseBody);
}
}
}

View File

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

View File

@ -6,6 +6,7 @@
"BasicWebSite": "",
"ActivatorWebSite": "",
"InlineConstraintsWebSite": "",
"AutofacWebSite": "",
"Microsoft.AspNet.TestHost": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Xunit.KRunner": "1.0.0-*",

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" 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>07c0e921-fcbb-458c-ac11-3d01ce68b16b</ProjectGuid>
<OutputType>Web</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>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DevelopmentServerPort>38823</DevelopmentServerPort>
</PropertyGroup>
<ItemGroup>
<Content Include="project.json" />
<Content Include="Views\Basic\Index.cshtml" />
<Content Include="Views\DI\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\BasicController.cs" />
<Compile Include="Controllers\DIController.cs" />
<Compile Include="HelloWorldBuilder.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,12 @@
using Microsoft.AspNet.Mvc;
namespace AutofacWebSite.Controllers
{
public class BasicController : Controller
{
public IActionResult Index()
{
return View();
}
}
}

View File

@ -0,0 +1,19 @@
using Microsoft.AspNet.Mvc;
namespace AutofacWebSite.Controllers
{
public class DIController : Controller
{
public DIController(HelloWorldBuilder builder)
{
Builder = builder;
}
public HelloWorldBuilder Builder { get; private set; }
public IActionResult Index()
{
return View(model: Builder.Build());
}
}
}

View File

@ -0,0 +1,10 @@
namespace AutofacWebSite
{
public class HelloWorldBuilder
{
public string Build()
{
return "Hello from builder.";
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using Autofac;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Autofac;
namespace AutofacWebSite
{
public class Startup
{
public void Configure(IBuilder app)
{
app.UseServices(services => {
services.AddMvc();
services.AddTransient<HelloWorldBuilder>();
var builder = new ContainerBuilder();
AutofacRegistration.Populate(builder,
services,
fallbackServiceProvider: app.ApplicationServices);
var container = builder.Build();
return container.Resolve<IServiceProvider>();
});
app.UseMvc(routes =>
{
// This default route is for running the project directly.
routes.MapRoute("default", "{controller=DI}/{action=Index}");
});
}
}
}

View File

@ -0,0 +1 @@
<p>Hello From Basic View</p>

View File

@ -0,0 +1,3 @@
@model string
<p>Builder Output: @Model</p>

View File

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