コード例 #1
0
        public static ServiceDescriptor AssertServiceDescriptor <TService>(
            IServiceCollection services,
            IServiceProvider provider,
            ServiceLifetime lifetime,
            ServiceDescriptorSearchOptions searchOptions = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            searchOptions ??= ServiceDescriptorSearchOptions.SearchByServiceType <TService>();

            IList <ServiceDescriptor> descriptors = searchOptions.Search(services).ToList();

            Assert.Equal(expected: 1, actual: descriptors.Count);

            ServiceDescriptor descriptor = descriptors[0];

            Assert.Equal(expected: lifetime, actual: descriptor.Lifetime);
            Assert.Equal(expected: typeof(TService), actual: descriptor.ServiceType);

            return(descriptor);
        }
コード例 #2
0
        public static TImplementation AssertService <TService, TImplementation>(
            IServiceCollection services,
            IServiceProvider provider,
            ServiceLifetime lifetime,
            Action <TImplementation> assert = null,
            ServiceDescriptorSearchOptions searchOptions = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            ServiceDescriptor descriptor = AssertServiceDescriptor <TService>(
                services,
                provider,
                lifetime,
                searchOptions);

            object service = GetService(provider, descriptor);

            Assert.NotNull(service);
            Assert.IsType <TImplementation>(service);
            Assert.IsAssignableFrom(descriptor.ServiceType, service);

            var result = (TImplementation)service;

            assert?.Invoke(result);

            return(result);
        }
コード例 #3
0
        public static TException AssertServiceThrows <TService, TImplementation, TException>(
            IServiceCollection services,
            IServiceProvider provider,
            ServiceLifetime lifetime,
            Action <TException> assert = null,
            ServiceDescriptorSearchOptions searchOptions = null)
            where TException : Exception
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            ServiceDescriptor descriptor = AssertServiceDescriptor <TService>(
                services,
                provider,
                lifetime,
                searchOptions);

            Assert.True(descriptor.ServiceType.IsAssignableFrom(typeof(TImplementation)));

            TException exception = Assert.Throws <TException>(() => GetService(provider, descriptor));

            assert?.Invoke(exception);

            return(exception);
        }
コード例 #4
0
 public static TService AssertService <TService>(
     IServiceCollection services,
     IServiceProvider provider,
     ServiceLifetime lifetime,
     Action <TService> assert = null,
     ServiceDescriptorSearchOptions searchOptions = null)
 {
     return(AssertService <TService, TService>(
                services,
                provider,
                lifetime,
                assert,
                searchOptions));
 }
コード例 #5
0
 public static TException AssertServiceThrows <TService, TException>(
     IServiceCollection services,
     IServiceProvider provider,
     ServiceLifetime lifetime,
     Action <TException> assert = null,
     ServiceDescriptorSearchOptions searchOptions = null)
     where TException : Exception
 {
     return(AssertServiceThrows <TService, TService, TException>(
                services,
                provider,
                lifetime,
                assert,
                searchOptions));
 }