Simplifying InlineConstraints tests

Right now these use a commandline adapter to inject some data into the
tests, but it's really not needed. Instead, these routes use a prefix to
ensure that the scenario under test is isolated.
This commit is contained in:
Ryan Nowak 2014-11-20 17:36:10 -08:00
parent ba1d66d683
commit e54bf866e0
5 changed files with 11 additions and 121 deletions

View File

@ -7,37 +7,24 @@ using System.Threading.Tasks;
using InlineConstraints; using InlineConstraints;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost; using Microsoft.AspNet.TestHost;
using Microsoft.Framework.DependencyInjection;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.FunctionalTests namespace Microsoft.AspNet.Mvc.FunctionalTests
{ {
public class InlineConstraintTests public class InlineConstraintTests
{ {
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider = TestHelper.CreateServices("InlineConstraintsWebSite");
private readonly Action<IApplicationBuilder> _app = new Startup().Configure; private readonly Action<IApplicationBuilder> _app = new Startup().Configure;
public InlineConstraintTests()
{
var services = new ServiceCollection()
.AddScoped<ICommandLineArgumentBuilder, DefaultCommandLineArgumentBuilder>();
_provider = TestHelper.CreateServices("InlineConstraintsWebSite", services);
}
[Fact] [Fact]
public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction() public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction()
{ {
var svc = _provider.GetRequiredService<ICommandLineArgumentBuilder>(); // Arrange
svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" +
"{area:exists}/{controller=Home}/{action=Index}");
svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue=" +
"{controller=Home}/{action=Index}");
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.CreateClient(); var client = server.CreateClient();
// Act // Act
var response = await client.GetAsync("http://localhost/Users"); var response = await client.GetAsync("http://localhost/area-exists/Users");
// Assert // Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -49,17 +36,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
public async Task RoutingToANonExistantArea_WithoutExistConstraint_RoutesToIncorrectAction() public async Task RoutingToANonExistantArea_WithoutExistConstraint_RoutesToIncorrectAction()
{ {
// Arrange // Arrange
var svc = _provider.GetRequiredService<ICommandLineArgumentBuilder>();
svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" +
"{area}/{controller=Home}/{action=Index}");
svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue" +
"={controller=Home}/{action=Index}");
var server = TestServer.Create(_provider, _app); var server = TestServer.Create(_provider, _app);
var client = server.CreateClient(); var client = server.CreateClient();
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/Users")); var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://localhost/area-withoutexists/Users"));
Assert.Equal("The view 'Index' was not found." + Assert.Equal("The view 'Index' was not found." +
" The following locations were searched:\r\n/Areas/Users/Views/Home/Index.cshtml\r\n" + " The following locations were searched:\r\n/Areas/Users/Views/Home/Index.cshtml\r\n" +
@ -67,4 +48,4 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
ex.Message); ex.Message);
} }
} }
} }

View File

@ -1,10 +0,0 @@
{
"TemplateCollection" : {
"areaRoute" : {
"TemplateValue" : "{area:exists}/{controller=Home}/{action=Index}"
},
"actionRoute" : {
"TemplateValue" : "{controller=Home}/{action=Index}"
}
}
}

View File

@ -1,22 +0,0 @@
// 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.Collections.Generic;
namespace InlineConstraints
{
public class DefaultCommandLineArgumentBuilder : ICommandLineArgumentBuilder
{
private readonly List<string> _args = new List<string>();
public void AddArgument(string arg)
{
_args.Add(arg);
}
public IEnumerable<string> Build()
{
return _args;
}
}
}

View File

@ -1,14 +0,0 @@
// 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.Collections.Generic;
namespace InlineConstraints
{
public interface ICommandLineArgumentBuilder
{
void AddArgument(string arg);
IEnumerable<string> Build();
}
}

View File

@ -2,14 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Runtime;
namespace InlineConstraints namespace InlineConstraints
{ {
@ -21,59 +16,19 @@ namespace InlineConstraints
{ {
var configuration = app.GetTestConfiguration(); var configuration = app.GetTestConfiguration();
configuration.AddEnvironmentVariables();
var commandLineBuilder = app.ApplicationServices.GetRequiredService<ICommandLineArgumentBuilder>();
string appConfigPath;
if (configuration.TryGet("AppConfigPath", out appConfigPath))
{
configuration.AddJsonFile(appConfigPath);
}
else if (commandLineBuilder != null)
{
var args = commandLineBuilder.Build();
configuration.AddCommandLine(args.ToArray());
}
else
{
var basePath = app.ApplicationServices.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
configuration.AddJsonFile(Path.Combine(basePath, @"App_Data\config.json"));
}
// Set up application services
app.UseServices(services => app.UseServices(services =>
{ {
// Add MVC services to the services container
services.AddMvc(configuration); services.AddMvc(configuration);
}); });
// Add MVC to the request pipeline
app.UseMvc(routes => app.UseMvc(routes =>
{
foreach (var item in GetDataFromConfig(configuration))
{
routes.MapRoute(item.RouteName, item.RouteTemplateValue);
}
});
}
private IEnumerable<RouteConfigData> GetDataFromConfig(IConfiguration config)
{
foreach (var template in config.GetSubKey("TemplateCollection").GetSubKeys())
{ {
yield return // Used by tests for the 'exists' constraint.
new RouteConfigData() routes.MapRoute("areaExists-area", "area-exists/{area:exists}/{controller=Home}/{action=Index}");
{ routes.MapRoute("areaExists", "area-exists/{controller=Home}/{action=Index}");
RouteName = template.Key, routes.MapRoute("areaWithoutExists-area", "area-withoutexists/{area}/{controller=Home}/{action=Index}");
RouteTemplateValue = template.Value.Get("TemplateValue") routes.MapRoute("areaWithoutExists", "area-withoutexists/{controller=Home}/{action=Index}");
}; });
}
}
private class RouteConfigData
{
public string RouteName { get; set; }
public string RouteTemplateValue { get; set; }
} }
} }
} }