public void WebApi_DependencyResolver_GetService_should_return_null_when_unresolved() { HttpConfiguration unitTestWebApiConfiguration = new HttpConfiguration(); ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { }) .AddWebApiDependencyResolver(unitTestWebApiConfiguration) .Build(); using ( di ) { DependencyInjectionWebApiDependencyResolver webApiResolverInstance = di.GetClients() .OfType <DependencyInjectionWebApiDependencyResolver>() .Single(); // IDependencyResolver webApiResolver = webApiResolverInstance; // { Object implementation = webApiResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } // And call it again for good measure: { Object implementation = webApiResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } } }
public void WebApi_DependencyResolver_GetServices_should_return_empty_enumerable_when_unresolved() { HttpConfiguration unitTestWebApiConfiguration = new HttpConfiguration(); ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { }) .AddWebApiDependencyResolver(unitTestWebApiConfiguration) .Build(); using ( di ) { DependencyInjectionWebApiDependencyResolver webApiResolverInstance = di.GetClients() .OfType <DependencyInjectionWebApiDependencyResolver>() .Single(); // IDependencyResolver webApiResolver = webApiResolverInstance; // IEnumerable <Object> services = webApiResolver.GetServices(typeof(INonRegisteredService)); _ = services.ShouldNotBeNull(); services.Count().ShouldBe(expected: 0); // Check twice, in case of IEnumerable<T> implementation shenanigans: services.Count().ShouldBe(expected: 0); } }
public void Mvc_DependencyResolver_GetService_should_return_null_for_unregistered_types() { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { }) .AddMvcDependencyResolver() .Build(); using ( di ) { DependencyInjectionMvcDependencyResolver mvcResolverInstance = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Single(); // IDependencyResolver mvcResolver = mvcResolverInstance; // { Object implementation = mvcResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } // And call it again for good measure: { Object implementation = mvcResolver.GetService(typeof(INonRegisteredService)); implementation.ShouldBeNull(); } } }
public void AddMvcDependencyResolver_should_add_single_DependencyInjectionWebApiDependencyResolver() { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .AddMvcDependencyResolver() .Build(); using ( di ) { Int32 count = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Count(); count.ShouldBe(expected: 1); } }
public void AddWebApiDependencyResolver_should_add_single_DependencyInjectionWebApiDependencyResolver() { HttpConfiguration unitTestWebApiConfiguration = new HttpConfiguration(); ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .AddWebApiDependencyResolver(unitTestWebApiConfiguration) .Build(); using ( di ) { Int32 count = di.GetClients() .OfType <DependencyInjectionWebApiDependencyResolver>() .Count(); count.ShouldBe(expected: 1); } }
protected void WrapTest <T>(Action <IServiceCollection> configure, Action <IDependencyResolver, T> testImpl, T arg) { HttpConfiguration nonSystemWebConfiguration = new HttpConfiguration(); ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(configure) .AddWebApiDependencyResolver(nonSystemWebConfiguration) .Build(); using ( di ) { DependencyInjectionWebApiDependencyResolver webApiResolverInstance = di.GetClients() .OfType <DependencyInjectionWebApiDependencyResolver>() .Single(); IDependencyResolver webApiResolver = webApiResolverInstance; testImpl(webApiResolver, arg); } }
// See \AspNetWebStack\test\System.Web.Mvc.Test\Test\DependencyResolverTest.cs /// <summary>Don't call this method from tests that verify this logic works, like <see cref="AddWebApiDependencyResolver_should_add_single_DependencyInjectionWebApiDependencyResolver"/>.</summary> private static ApplicationDependencyInjection CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver mvcResolver) { ApplicationDependencyInjection di = new ApplicationDependencyInjectionBuilder() .ConfigureServices(services => { _ = services.AddTransient <ITestTransient, TestTransient>(); _ = services.AddScoped <ITestScoped, TestScoped>(); _ = services.AddSingleton <ITestSingleton, TestSingleton>(); _ = services.AddSingleton <TestAbstractClass, TestAbstractImpl>(); _ = services.AddSingleton(serviceType: typeof(ITestGeneric <>), implementationType: typeof(TestestGeneric <>)); }) .AddMvcDependencyResolver() .Build(); DependencyInjectionMvcDependencyResolver mvcResolverInstance = di.GetClients() .OfType <DependencyInjectionMvcDependencyResolver>() .Single(); mvcResolver = mvcResolverInstance; return(di); }