public void GetControllerType_WithMatchedDirectRoute_UseControllerDescriptorType() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteFromController <AttributeRouteAtControllerLevelController>(); requestContext.RouteData.AddDirectRouteMatches(); var expectedControllerType = typeof(AttributeRouteAtControllerLevelController); var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver <IControllerActivator>(); var factory = new DefaultControllerFactory( controllerActivator, activatorResolver, null ); // Act // if it would not get the controller type from the DirectRoute, then it would not // be able to find any controller var type = factory.GetControllerType(requestContext, controllerName: null); // Assert Assert.Equal(expectedControllerType, type); }
public void GetControllerType_WithMultipleDirectRouteControllers_ThrowsInvalidOperationException() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteFromController <AttributeRouteAtControllerLevelController>(); requestContext.RouteData.Route.AddDirectRouteFromMethod <WithRoutingAttributeController>(c => c.Action()); requestContext.RouteData.AddDirectRouteMatches(); var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver <IControllerActivator>(); var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null); // Act & Assert Assert.Throws <InvalidOperationException>(() => factory.GetControllerType(requestContext, controllerName: null)); }