单元测试IServiceCollection注册 [英] Unit Testing IServiceCollection Registration

查看:311
本文介绍了单元测试IServiceCollection注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到最简单的方法来测试我的框架的服务注册方法。我正在创建动态服务,我的注册看起来像这样:

I'm trying to figure out the easiest way to test my Service Registrations method for my framework. I'm creating dynamic services my registration looks like so:

var messageHubConfig = new DynamicHubServiceConfiguration<Message, MessageDTO>();
messageHubConfig.SetDynamicHubOptions<AstootContext>(async (context, dto) =>
{
    return await context.ConversationSubscriptions
                        .Where(x => x.ConversationId == dto.ConversationId 
                               && x.IsSubscribed)
                        .Distinct()
                        .Select(x => x.User.UniqueIdentifier)
                        .ToListAsync();
});

messageHubConfig.RequiresDynamicValidator = false;
messageHubConfig.EventMapping.AddCreateEvent(async (sp, obj, dto) =>
{
    var conversationService = sp.GetService<IRestEzService<Conversation, ConversationDTO>>();
    var conversationDTO = await conversationService.Get(new object[] { dto.ConversationId });
    var hubTaskQueue = sp.GetService<IHubServiceTaskQueue>();
    hubTaskQueue.QueueDynamicCreate(conversationDTO);
}).When(async (sp, dto) => {
    var context = sp.GetService<AstootContext>();
    return await context.Conversations.Where(x => x.Id == dto.ConversationId).Where(x => x.Messages.Count == 1).AnyAsync();
});

//Registers service with a hub
restConfiguration.RegisterRestService(typeof(IMessageDTOService), 
                                      typeof(MessageDTOService), 
                                      messageHubConfig);

在我的注册休息服务方法内,我有很多不同的服务正在注册,例如:

Inside of my Register Rest Service Method I have a lot of different services Getting registered e.g:

services.AddTransient(restServiceType, (IServiceProvider serviceProvider) =>
{
    var restService = (IRestEzService<TEntity, TDTO>)
        ActivatorUtilities.CreateInstance(serviceProvider, restServiceImplementationType);
    serviceOption.EventMapping?.Register(serviceProvider, restService);

    return restService;
});

如何确保我的工厂配置已正确注册,如何创建服务集合

How can I be assure that my factory configuration is being registered properly, How can I create a Service Collection for testing?

推荐答案

创建 ServiceCollection

var services = new ServiceCollection();

调用您的注册函数,然后断言您的 restServiceType 已添加。

call your registration function and then assert that your restServiceType was added.

接下来从服务集合中构建提供者,解析 restServiceType

Next build a provider from the service collection, resolve the restServiceType

var provider = services.BuildServiceProvider();
var restService = provider.GetRequiredService(restServiceType);

并断言它是根据需要创建的。

and assert that it is created as desired.

如果服务无法解析目标类型,则 GetRequiredService 扩展方法将引发异常。

The GetRequiredService extension method will throw an exception if the service is unable to resolve the target type.

现在,这完全基于您的示例中当前显示的内容,因为我不知道任何其他依赖项。

Now that is based solely on what is currently being shown in your example as I am unaware of any other dependencies.

这篇关于单元测试IServiceCollection注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆