This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-testsetup-cil/src/MfGames.TestSetup/TestContext.cs

70 lines
1.6 KiB
C#
Raw Normal View History

2021-09-10 17:12:49 +00:00
using System;
using Autofac;
using Serilog;
namespace MfGames.TestSetup
{
/// <summary>
/// A context for the test run that includes the container.
/// </summary>
public class TestContext : IDisposable
{
private IContainer? container;
private ILogger? logger;
public IContainer Container
{
get => this.container
?? throw new NullReferenceException(
"ConfigureContainer has not been called.");
private set => this.container = value;
}
public ILogger Logger
{
get => this.logger
?? throw new NullReferenceException(
"SetLogger has not been called.");
private set => this.logger = value;
}
public void ConfigureContainer()
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(this.Logger)
.As<ILogger>()
.SingleInstance();
this.ConfigureContainer(builder);
this.Container = builder.Build();
}
/// <inheritdoc />
public void Dispose()
{
this.container?.Dispose();
}
public TType Resolve<TType>()
where TType : class
{
return this.Container.Resolve<TType>();
}
public void SetLogger(ILogger value)
{
this.Logger = value;
}
protected virtual void ConfigureContainer(ContainerBuilder builder)
{
}
}
}