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/test/Volo.Abp.Tests/Volo/Abp/DependencyInjection/ConventionalRegistrar_Test.cs

50 lines
1.4 KiB

using System;
using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp.DependencyInjection
{
public class ConventionalRegistrar_Tests
{
public void Should_Use_Custom_Conventions_If_Added()
{
//Arrange
var services = new ServiceCollection();
//Act
services.AddConventionalRegistrar(new MyCustomConventionalRegistrar());
services.AddTypes(typeof(MyCustomClass), typeof(MyClass), typeof(MyNonRegisteredClass));
//Assert
services.ShouldContainTransient(typeof(MyClass)); //Registered by default convention.
services.ShouldContainSingleton(typeof(MyCustomClass)); //Registered by custom convention.
services.ShouldNotContainService(typeof(MyNonRegisteredClass)); //Not registered by any convention.
}
public class MyCustomConventionalRegistrar : ConventionalRegistrarBase
{
public override void AddType(IServiceCollection services, Type type)
{
if (type == typeof(MyClass))
{
services.AddSingleton<MyCustomClass>();
}
}
}
public class MyCustomClass
{
}
public class MyNonRegisteredClass
{
}
public class MyClass : ITransientDependency
{
}
}
}