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?
AddScoped(IServiceCollection, Type, Type) Adds a scoped service of the type specified in serviceType with an implementation of the type specified in implementationType to the specified IServiceCollection.
Unit testing breaks the program down into the smallest bit of code, usually function-level, and ensures that the function returns the value one expects. By using a unit testing framework, the unit tests become a separate entity which can then run automated tests on the program as it is being built.
Create a ServiceCollection
,
var services = new ServiceCollection();
call your registration function and then assert that your restServiceType
was added.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With