Add UseMvc unit tests (#8164)

This commit is contained in:
James Newton-King 2018-07-29 16:58:50 +12:00 committed by GitHub
parent fbae57cde1
commit 41de26a546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 63 additions and 0 deletions

View File

@ -2,7 +2,14 @@
// 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.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;
@ -49,5 +56,61 @@ namespace Microsoft.AspNetCore.Mvc.Core.Builder
"in the application startup code.",
exception.Message);
}
[Fact]
public void UseMvc_GlobalRoutingDisabled_NoEndpointInfos()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore"));
services.AddLogging();
services.AddMvcCore(o => o.EnableGlobalRouting = false);
var serviceProvider = services.BuildServiceProvider();
var appBuilder = new ApplicationBuilder(serviceProvider);
// Act
appBuilder.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
var mvcEndpointDataSource = appBuilder.ApplicationServices
.GetRequiredService<IEnumerable<EndpointDataSource>>()
.OfType<MvcEndpointDataSource>()
.First();
Assert.Empty(mvcEndpointDataSource.ConventionalEndpointInfos);
}
[Fact]
public void UseMvc_GlobalRoutingEnabled_NoEndpointInfos()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore"));
services.AddLogging();
services.AddMvcCore(o => o.EnableGlobalRouting = true);
var serviceProvider = services.BuildServiceProvider();
var appBuilder = new ApplicationBuilder(serviceProvider);
// Act
appBuilder.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
var mvcEndpointDataSource = appBuilder.ApplicationServices
.GetRequiredService<IEnumerable<EndpointDataSource>>()
.OfType<MvcEndpointDataSource>()
.First();
var endpointInfo = Assert.Single(mvcEndpointDataSource.ConventionalEndpointInfos);
Assert.Equal("default", endpointInfo.Name);
Assert.Equal("{controller=Home}/{action=Index}/{id?}", endpointInfo.Template);
}
}
}