private void RegisterFactories() { //Create a custom controller factory to resolve controllers with IoC container IControllerFactory factory = new IoCControllerFactory(_CurrentContainer); //Set new controller factory in ASP.MVC Controller builder ControllerBuilder.Current.SetControllerFactory(factory); }
public void And_default_resolver_is_null_then_ReleaseImplementation_should_not_be_called_on_the_default_resolver() { DependencyResolver.InitializeWith(null); var controller = new IocTestController(); IControllerFactory factory = new IoCControllerFactory(); factory.ReleaseController(controller); }
public void And_controller_implements_IDisposable_then_dispose_should_be_called() { var controller = new DisposableIocTestController(); IControllerFactory factory = new IoCControllerFactory(_dependencyResolver); factory.ReleaseController(controller); Assert.IsTrue(controller.IsDisposed); }
public void Then_ReleaseImplementation_should_be_called_on_the_specified_resolver() { var controller = new IocTestController(); _dependencyResolver.Expect(r => r.DisposeImplementation(controller)); IControllerFactory factory = new IoCControllerFactory(_dependencyResolver); factory.ReleaseController(controller); }
static void RegisterFactories() { //Create a custom controller factory to resolve controllers with IoC container IControllerFactory factory = new IoCControllerFactory(_CurrentContainer); //Set new controller factory in ASP.MVC Controller builder ControllerBuilder.Current.SetControllerFactory(factory); GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(_CurrentContainer); }
public void Then_ReleaseImplementation_should_be_called_on_the_default_resolver() { DependencyResolver.InitializeWith(_dependencyResolver); var controller = new IocTestController(); _dependencyResolver.Expect(r => r.DisposeImplementation(controller)); IControllerFactory factory = new IoCControllerFactory(); factory.ReleaseController(controller); }
public void Should_throw_if_controller_type_cannot_be_resolved() { var requestContext = new RequestContext(MvcMockHelpers.DynamicHttpContextBase(), new RouteData()); //HttpException(0x194, IControllerFactory controllerFactory = new IoCControllerFactory(_dependencyResolver); controllerFactory.InitializeWithControllerTypes(typeof(IocTestController)); controllerFactory.CreateController(requestContext, "DoesNotExist"); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AutoMapperConfig.RegisterMapping(); var controllerFactory = new IoCControllerFactory(DependencyConfig.RegisterTypes()); ControllerBuilder.Current.SetControllerFactory(controllerFactory); }
public void Should_call_into_the_static_resolver_to_create_a_controller() { var requestContext = new RequestContext(MvcMockHelpers.DynamicHttpContextBase(), new RouteData()); IControllerFactory controllerFactory = new IoCControllerFactory(); controllerFactory.InitializeWithControllerTypes(typeof(IocTestController)); IController controller = controllerFactory.CreateController(requestContext, "IocTest"); Assert.That(controller, Is.TypeOf(typeof(IocTestController))); }
public void Should_call_into_the_resolver_to_create_a_controller() { var requestContext = new RequestContext(MvcMockHelpers.DynamicHttpContextBase(), new RouteData()); _dependencyResolver.Expect(r => r.GetImplementationOf <IController>(typeof(IocTestController))) .Return(new IocTestController()); _dependencyResolver.Replay(); IControllerFactory controllerFactory = new IoCControllerFactory(_dependencyResolver); controllerFactory.InitializeWithControllerTypes(typeof(IocTestController)); IController controller = controllerFactory.CreateController(requestContext, "IocTest"); Assert.That(controller, Is.TypeOf(typeof(IocTestController))); _dependencyResolver.VerifyAllExpectations(); }
public void Get_Controller_Instance_Throws_Null_Reference_Exception_When_The_Controller_Type_Is_Null() { //Arrange SIContainer serviceFactory = new SIContainer(); HomeController controller = new HomeController(); serviceFactory.ResolveType = x => controller; IoCControllerFactory controllerFactory = new IoCControllerFactory(serviceFactory); MDefaultControllerFactory baseMole = new MDefaultControllerFactory(controllerFactory); IoCControllerFactory_Accessor factoryAccessor = new IoCControllerFactory_Accessor(new PrivateObject(controllerFactory)); baseMole.CreateControllerRequestContextString = (context, name) => factoryAccessor.GetControllerInstance(context, null); //Act HomeController resolvedController = controllerFactory.CreateController(null, "Home") as HomeController; }
public void Get_Controller_Instance_Returns_The_Right_Controller() { //Arrange SIContainer container = new SIContainer(); HomeController controller = new HomeController(); container.ResolveType = x => controller; IoCControllerFactory controllerFactory = new IoCControllerFactory(container); MDefaultControllerFactory baseMole = new MDefaultControllerFactory(controllerFactory); //Create a MSTest accessor to access GetControllerInstance IoCControllerFactory_Accessor factoryAccessor = new IoCControllerFactory_Accessor(new PrivateObject(controllerFactory)); baseMole.CreateControllerRequestContextString = (context, name) => factoryAccessor.GetControllerInstance(context, typeof(HomeController)); //Act HomeController resolvedController = controllerFactory.CreateController(null, "Home") as HomeController; //Assert Assert.AreSame(controller, resolvedController); }
public void Should_throw_if_controllerName_is_null() { IControllerFactory controllerFactory = new IoCControllerFactory(_dependencyResolver); controllerFactory.CreateController(null, null); }
public void Should_throw_an_argument_null_exception_when_the_resolver_is_null() { IControllerFactory controllerFactory = new IoCControllerFactory(null); }