You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
abp/framework/test/AbpTestBase/Microsoft/Extensions/DependencyInjection/ServiceCollectionShouldlyEx...

71 lines
3.1 KiB

using System;
using System.Linq;
using Shouldly;
namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionShouldlyExtensions
{
public static void ShouldContainTransient(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Transient);
}
public static void ShouldContainTransientImplementationFactory(this IServiceCollection services, Type serviceType)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBeNull();
serviceDescriptor.ImplementationFactory.ShouldNotBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Transient);
}
public static void ShouldContainSingleton(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Singleton);
}
public static void ShouldContainScoped(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Scoped);
}
public static void ShouldContain(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(implementationType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(lifetime);
}
public static void ShouldNotContainService(this IServiceCollection services, Type serviceType)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldBeNull();
}
}