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:
parent
ba1d66d683
commit
e54bf866e0
|
|
@ -7,37 +7,24 @@ using System.Threading.Tasks;
|
|||
using InlineConstraints;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||
{
|
||||
public class InlineConstraintTests
|
||||
{
|
||||
private readonly IServiceProvider _provider;
|
||||
private readonly IServiceProvider _provider = TestHelper.CreateServices("InlineConstraintsWebSite");
|
||||
private readonly Action<IApplicationBuilder> _app = new Startup().Configure;
|
||||
|
||||
public InlineConstraintTests()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddScoped<ICommandLineArgumentBuilder, DefaultCommandLineArgumentBuilder>();
|
||||
_provider = TestHelper.CreateServices("InlineConstraintsWebSite", services);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction()
|
||||
{
|
||||
var svc = _provider.GetRequiredService<ICommandLineArgumentBuilder>();
|
||||
svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue=" +
|
||||
"{area:exists}/{controller=Home}/{action=Index}");
|
||||
svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue=" +
|
||||
"{controller=Home}/{action=Index}");
|
||||
|
||||
// Arrange
|
||||
var server = TestServer.Create(_provider, _app);
|
||||
var client = server.CreateClient();
|
||||
|
||||
// Act
|
||||
var response = await client.GetAsync("http://localhost/Users");
|
||||
var response = await client.GetAsync("http://localhost/area-exists/Users");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
|
|
@ -49,17 +36,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
public async Task RoutingToANonExistantArea_WithoutExistConstraint_RoutesToIncorrectAction()
|
||||
{
|
||||
// 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 client = server.CreateClient();
|
||||
|
||||
// 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." +
|
||||
" 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"TemplateCollection" : {
|
||||
"areaRoute" : {
|
||||
"TemplateValue" : "{area:exists}/{controller=Home}/{action=Index}"
|
||||
},
|
||||
"actionRoute" : {
|
||||
"TemplateValue" : "{controller=Home}/{action=Index}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,14 +2,9 @@
|
|||
// 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.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Runtime;
|
||||
|
||||
namespace InlineConstraints
|
||||
{
|
||||
|
|
@ -21,59 +16,19 @@ namespace InlineConstraints
|
|||
{
|
||||
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 =>
|
||||
{
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc(configuration);
|
||||
});
|
||||
|
||||
// Add MVC to the request pipeline
|
||||
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
|
||||
new RouteConfigData()
|
||||
{
|
||||
RouteName = template.Key,
|
||||
RouteTemplateValue = template.Value.Get("TemplateValue")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private class RouteConfigData
|
||||
{
|
||||
public string RouteName { get; set; }
|
||||
public string RouteTemplateValue { get; set; }
|
||||
// Used by tests for the 'exists' constraint.
|
||||
routes.MapRoute("areaExists-area", "area-exists/{area:exists}/{controller=Home}/{action=Index}");
|
||||
routes.MapRoute("areaExists", "area-exists/{controller=Home}/{action=Index}");
|
||||
routes.MapRoute("areaWithoutExists-area", "area-withoutexists/{area}/{controller=Home}/{action=Index}");
|
||||
routes.MapRoute("areaWithoutExists", "area-withoutexists/{controller=Home}/{action=Index}");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue