Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Add more to MvcCoreServiceCollectionExtensionsTest #5564

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static ApplicationPartManager GetApplicationPartManager(IServiceCollecti
private static T GetServiceFromCollection<T>(IServiceCollection services)
{
return (T)services
.FirstOrDefault(d => d.ServiceType == typeof(T))
.LastOrDefault(d => d.ServiceType == typeof(T))
?.ImplementationInstance;
}

Expand Down Expand Up @@ -131,7 +131,7 @@ internal static void AddMvcCoreServices(IServiceCollection services)
//
// Action Discovery
//
// These are consumed only when creating action descriptors, then they can be de-allocated
// These are consumed only when creating action descriptors, then they can be deallocated

services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, DefaultApplicationModelProvider>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
Expand All @@ -32,7 +36,7 @@ public void MultiRegistrationServiceTypes_AreRegistered_MultipleTimes()
// Arrange
var services = new ServiceCollection();

// Register a mock implementation of each service, AddMvcServices should add another implemenetation.
// Register a mock implementation of each service, AddMvcServices should add another implementation.
foreach (var serviceType in MutliRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType.Key);
Expand Down Expand Up @@ -104,6 +108,112 @@ public void AddMvcServicesTwice_DoesNotAddDuplicates()
}
}

[Fact]
public void AddMvcCore_UsesOriginalPartManager()
{
// Arrange
var manager = new ApplicationPartManager();
var services = new ServiceCollection();
services.AddSingleton(manager);

// Act
var builder = services.AddMvcCore();

// Assert
// SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes already checks that no other
// ApplicationPartManager (but manager) is registered.
Assert.Same(manager, builder.PartManager);
Assert.Contains(manager.FeatureProviders, provider => provider is ControllerFeatureProvider);
}

// Regression test for aspnet/Mvc#5554.
[Fact]
public void AddMvcCore_UsesLastPartManager()
{
// Arrange
var services = new ServiceCollection();
var mockManager = new Mock<ApplicationPartManager>(MockBehavior.Strict);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do so many mocks have to be added? Would it be enough to add 1 fake one, then 1 real one, and assert that the 1 real one (which was last) was retrieved?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to test 1 and 3 rather than 1 and 2. Either is fine and I'll remove the two extra lines in two tests.

services.AddSingleton(mockManager.Object);

var manager = new ApplicationPartManager();
services.AddSingleton(manager);

// Act
var builder = services.AddMvcCore();

// Assert
Assert.Same(manager, builder.PartManager);
Assert.Contains(manager.FeatureProviders, provider => provider is ControllerFeatureProvider);
}

[Fact]
public void AddMvcCore_UsesOriginalHostingEnvironment()
{
// Arrange
var services = new ServiceCollection();
var environment = new Mock<IHostingEnvironment>(MockBehavior.Strict);
environment.SetupGet(e => e.ApplicationName).Returns((string)null).Verifiable();
services.AddSingleton<IHostingEnvironment>(environment.Object);

// Act
var builder = services.AddMvcCore();

// Assert
Assert.NotNull(builder.PartManager);
Assert.Empty(builder.PartManager.ApplicationParts);
Assert.Contains(builder.PartManager.FeatureProviders, provider => provider is ControllerFeatureProvider);

environment.VerifyAll();
}

// Second regression test for aspnet/Mvc#5554.
[Fact]
public void AddMvcCore_UsesLastHostingEnvironment()
{
// Arrange
var services = new ServiceCollection();
var environment = new Mock<IHostingEnvironment>(MockBehavior.Strict);
services.AddSingleton<IHostingEnvironment>(environment.Object);

environment = new Mock<IHostingEnvironment>(MockBehavior.Strict);
environment.SetupGet(e => e.ApplicationName).Returns((string)null).Verifiable();
services.AddSingleton<IHostingEnvironment>(environment.Object);

// Act
var builder = services.AddMvcCore();

// Assert
Assert.NotNull(builder.PartManager);
Assert.Empty(builder.PartManager.ApplicationParts);
Assert.Contains(builder.PartManager.FeatureProviders, provider => provider is ControllerFeatureProvider);

environment.VerifyAll();
}

[Fact]
public void AddMvcCore_GetsPartsForApplication()
{
// Arrange
var services = new ServiceCollection();
var environment = new Mock<IHostingEnvironment>(MockBehavior.Strict);
var assemblyName = typeof(MvcCoreServiceCollectionExtensionsTest).GetTypeInfo().Assembly.GetName();
var applicationName = assemblyName.FullName;
environment.SetupGet(e => e.ApplicationName).Returns(applicationName).Verifiable();
services.AddSingleton<IHostingEnvironment>(environment.Object);

// Act
var builder = services.AddMvcCore();

// Assert
Assert.NotNull(builder.PartManager);
Assert.Contains(
builder.PartManager.ApplicationParts,
part => string.Equals(assemblyName.Name, part.Name, StringComparison.Ordinal));
Assert.Contains(builder.PartManager.FeatureProviders, provider => provider is ControllerFeatureProvider);

environment.VerifyAll();
}

private IEnumerable<Type> SingleRegistrationServiceTypes
{
get
Expand Down