src/OneBeyond.Studio.Infrastructure.Azure.Tests/TestsBase.cs
using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace OneBeyond.Studio.Infrastructure.Azure.Tests;
public abstract class TestsBase
{
private IServiceScope? _serviceScope;
protected IServiceProvider? ServiceProvider { get; private set; }
[TestInitialize]
public void InitializeTest()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IConfiguration>(configuration);
ConfigureTestServices(configuration, serviceCollection);
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
ConfigureTestServices(configuration, containerBuilder);
var serviceProvider = new AutofacServiceProvider(containerBuilder.Build());
_serviceScope = serviceProvider.CreateScope();
ServiceProvider = _serviceScope.ServiceProvider;
}
[TestCleanup]
public void CleanupTest()
{
ServiceProvider = null;
_serviceScope?.Dispose();
_serviceScope = null;
}
protected abstract void ConfigureTestServices(
IConfiguration configuration,
IServiceCollection serviceCollection);
protected abstract void ConfigureTestServices(
IConfiguration configuration,
ContainerBuilder containerBuilder);
}