From 41de26a546d565e28e757df371204348dc887dbf Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 29 Jul 2018 16:58:50 +1200 Subject: [PATCH] Add UseMvc unit tests (#8164) --- .../MvcApplicationBuilderExtensionsTest.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs index 3b2f75f341..8e4c7820f6 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs @@ -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(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>() + .OfType() + .First(); + + Assert.Empty(mvcEndpointDataSource.ConventionalEndpointInfos); + } + + [Fact] + public void UseMvc_GlobalRoutingEnabled_NoEndpointInfos() + { + // Arrange + var services = new ServiceCollection(); + services.AddSingleton(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>() + .OfType() + .First(); + + var endpointInfo = Assert.Single(mvcEndpointDataSource.ConventionalEndpointInfos); + Assert.Equal("default", endpointInfo.Name); + Assert.Equal("{controller=Home}/{action=Index}/{id?}", endpointInfo.Template); + } } }