public void ViewLocationFormats_ContainsExpectedLocations() { // Arrange var services = new ServiceCollection().AddOptions(); var viewLocations = new[] { "/MvcViews/{1}/{0}.cshtml", "/MvcViews/Shared/{0}.cshtml" }; var builder = new MvcBuilder(services, new ApplicationPartManager()); builder.AddRazorOptions(options => { options.ViewLocationFormats.Clear(); foreach (var location in viewLocations) { options.ViewLocationFormats.Add(location); } }); var serviceProvider = services.BuildServiceProvider(); var accessor = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>(); // Act var formats = accessor.Value.ViewLocationFormats; // Assert Assert.Equal(viewLocations, formats, StringComparer.Ordinal); }
public void AddApplicationPart_AddsAnApplicationPart_ToTheListOfPartsOnTheBuilder() { // Arrange var manager = new ApplicationPartManager(); var builder = new MvcBuilder(Mock.Of<IServiceCollection>(), manager); var assembly = typeof(MvcBuilder).GetTypeInfo().Assembly; // Act var result = builder.AddApplicationPart(assembly); // Assert Assert.Same(result, builder); var part = Assert.Single(builder.PartManager.ApplicationParts); var assemblyPart = Assert.IsType<AssemblyPart>(part); Assert.Equal(assembly, assemblyPart.Assembly); }
public void AddRazorOptions_ConfiguresOptionsAsExpected() { // Arrange var services = new ServiceCollection().AddOptions(); var fileProvider = new TestFileProvider(); // Act var builder = new MvcBuilder(services, new ApplicationPartManager()); builder.AddRazorOptions(options => { options.FileProviders.Add(fileProvider); }); var serviceProvider = services.BuildServiceProvider(); // Assert var accessor = serviceProvider.GetRequiredService<IOptions<RazorViewEngineOptions>>(); Assert.Same(fileProvider, accessor.Value.FileProviders[0]); }
public void ConfigureApplicationParts_InvokesSetupAction() { // Arrange var builder = new MvcBuilder( Mock.Of<IServiceCollection>(), new ApplicationPartManager()); var part = new TestApplicationPart(); // Act var result = builder.ConfigureApplicationPartManager(manager => { manager.ApplicationParts.Add(part); }); // Assert Assert.Same(result, builder); Assert.Equal(new ApplicationPart[] { part }, builder.PartManager.ApplicationParts.ToArray()); }
public void AddControllerAsServices_MultipleCalls_RetainsPreviouslyAddedTypes() { // Arrange var services = new ServiceCollection(); var manager = new ApplicationPartManager(); manager.ApplicationParts.Add(new TestApplicationPart(typeof(ControllerOne), typeof(ControllerTwo))); manager.FeatureProviders.Add(new TestFeatureProvider()); var builder = new MvcBuilder(services, manager); builder.AddControllersAsServices(); // Act builder.AddControllersAsServices(); // Assert 2 var collection = services.ToList(); Assert.Equal(3, collection.Count); Assert.Single(collection, d => d.ServiceType.Equals(typeof(ControllerOne))); Assert.Single(collection, d => d.ServiceType.Equals(typeof(ControllerTwo))); }
public void WithControllersAsServices_AddsTypesToControllerTypeProviderAndServiceCollection() { // Arrange var collection = new ServiceCollection(); var controllerTypes = new[] { typeof(ControllerTypeA), typeof(TypeBController), }.Select(t => t.GetTypeInfo()).ToArray(); var builder = new MvcBuilder(collection, GetApplicationPartManager(controllerTypes)); // Act builder.AddControllersAsServices(); // Assert var services = collection.ToList(); Assert.Equal(3, services.Count); Assert.Equal(typeof(ControllerTypeA), services[0].ServiceType); Assert.Equal(typeof(ControllerTypeA), services[0].ImplementationType); Assert.Equal(ServiceLifetime.Transient, services[0].Lifetime); Assert.Equal(typeof(TypeBController), services[1].ServiceType); Assert.Equal(typeof(TypeBController), services[1].ImplementationType); Assert.Equal(ServiceLifetime.Transient, services[1].Lifetime); Assert.Equal(typeof(IControllerActivator), services[2].ServiceType); Assert.Equal(typeof(ServiceBasedControllerActivator), services[2].ImplementationType); Assert.Equal(ServiceLifetime.Transient, services[2].Lifetime); }