Home > Blockchain >  .NET 6 Unit Test with DependencyResolverHelper
.NET 6 Unit Test with DependencyResolverHelper

Time:11-25

I was using my unit tests on .Net5(or lower) with DependencyResolverHelper like this below. This is my base test class

public abstract class BaseTestClass
    {
        protected DependencyResolverHelper _serviceProvider;

        public BaseTestClass()
        {
            var webHost = WebHost.CreateDefaultBuilder()
                .UseStartup<Startup>()
                .Build();

            _serviceProvider = new DependencyResolverHelper(webHost);
        }
    }

and my DependencyResolverHelper

public class DependencyResolverHelper
    {
        private readonly IWebHost _webHost;

        /// <inheritdoc />
        public DependencyResolverHelper(IWebHost webHost) => _webHost = webHost;


        public T GetService<T>()
        {
            var serviceScope = _webHost.Services.CreateScope();
            var services = serviceScope.ServiceProvider;
            try
            {
                var scopedService = services.GetRequiredService<T>();
                return scopedService;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

but im bit confused with new .NET 6 Dependency things. Does anyone tried it without startup class?

I tried to change it with

WebApplicationBuilder

but it gave the No service for type 'MinimalAPI.Services.TokenService.ITokenService' has been registered. error.

CodePudding user response:

Just Because this part of codes .UseStartup<Startup>() called startup class and registed the services for you,If you try with WebApplicationBuilder in .net 6,You have to regist the services yourself,

I tried as below:

public void Test1()
        {
            var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { });

            builder.Services.AddSingleton<ISomeService,SomeService>();

            var app = builder.Build();

            var serviceProvider = new DependencyResolverHelper(app);

            var someservice = serviceProvider.GetService<ISomeService>();

            Assert.Equal(typeof(SomeService), someservice.GetType());
            
        }

DependencyResolverHelper class:

public class DependencyResolverHelper
    {
        private readonly WebApplication _app;

        /// <inheritdoc />
        public DependencyResolverHelper(WebApplication app) => _app = app;


        public T GetService<T>()
        {
            var serviceScope = _app.Services.CreateScope();
            var services = serviceScope.ServiceProvider;
            try
            {
                var scopedService = services.GetRequiredService<T>();
                return scopedService;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

Result:

enter image description here

  • Related