public void AddDefaultAspNetServices_WithNullHttpApplication_ExceptionThrown() { Action act = () => { HttpApplicationExtensions.AddDefaultAspNetServices <HttpApplication>(Mock.Of <IServiceCollection>(), null); }; act.Should().Throw <ArgumentNullException>() .And.ParamName.Should().Be("application"); }
public void AddDefaultAspNetServices() { // Creates a HttpContext var application = new HttpApplicationMock(); var request = new HttpRequest("The filename", "http://theurl", ""); var response = new HttpResponse(new StringWriter()); var session = FormatterServices.GetUninitializedObject(typeof(HttpSessionState)); HttpContext.Current = new HttpContext(request, response) { ApplicationInstance = application, Items = { { "AspSession", session } } }; try { var collection = new ServiceCollection(); var returnedCollection = HttpApplicationExtensions.AddDefaultAspNetServices(collection, application); returnedCollection.Should().BeSameAs(collection); // Builds the service provider and check the registered services var serviceProvider = collection.BuildServiceProvider(); // Checks the services serviceProvider.GetService(typeof(HttpApplication)).Should().BeSameAs(application); serviceProvider.GetService(typeof(HttpApplicationMock)).Should().BeSameAs(application); serviceProvider.GetService(typeof(HttpRequest)).Should().BeSameAs(request); serviceProvider.GetService(typeof(HttpResponse)).Should().BeSameAs(response); serviceProvider.GetService(typeof(HttpSessionState)).Should().BeSameAs(session); } finally { HttpContext.Current = null; } }