public void Stop_UsingServiceCollection() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IDependentService, DependentService>(); var serviceProviderAdapter = new ServiceProviderAdapter(serviceCollection, null); var result = serviceProviderAdapter.GetService(typeof(IDependentService)).As <DependentService>(); result.Should().NotBeNull(); result.DisposeCalled.Should().BeFalse(); // Stop() the adapter, it is mean we dispose the provider serviceProviderAdapter.Stop(true); result.DisposeCalled.Should().BeTrue(); // Check the ObjectDisposedException if calling the GetService() method serviceProviderAdapter.Invoking(spa => spa.GetService(typeof(IDependentService))).Should().ThrowExactly <ObjectDisposedException>() .And.ObjectName.Should().Be("PosInformatique.AspNet.WebForms.DependencyInjection.ServiceProviderAdapter"); }
public void Stop_UsingServiceProvider() { var dependentService = Mock.Of <IDependentService>(); var serviceProvider = new Mock <IServiceProvider>(MockBehavior.Strict); serviceProvider.As <IDisposable>(); serviceProvider.Setup(sp => sp.GetService(typeof(IDependentService))) .Returns(dependentService); var serviceProviderAdapter = new ServiceProviderAdapter(serviceProvider.Object, null); serviceProviderAdapter.GetService(typeof(IDependentService)).As <DependentService>(); // Stop() the adapter, it is mean we dispose the provider BUT the no serviceProviderAdapter.Stop(true); // Verify the Dispose() method is not called for a IServiceProvider which implement the IDisposable interface serviceProvider.As <IDisposable>().Verify(sp => sp.Dispose(), Times.Never); // Check the ObjectDisposedException if calling the GetService() method serviceProviderAdapter.Invoking(spa => spa.GetService(typeof(IDependentService))).Should().ThrowExactly <ObjectDisposedException>() .And.ObjectName.Should().Be("PosInformatique.AspNet.WebForms.DependencyInjection.ServiceProviderAdapter"); }