Skip to content

Instantly share code, notes, and snippets.

@shoaibshakeel381
Last active July 11, 2023 14:30
Show Gist options
  • Save shoaibshakeel381/6849807f8aa4efc85296b4dce065d603 to your computer and use it in GitHub Desktop.
Save shoaibshakeel381/6849807f8aa4efc85296b4dce065d603 to your computer and use it in GitHub Desktop.
sample code for using Autofac container for MsTest
public abstract class IntegrationTestsBase : IDisposable
{
protected IntegrationTestsBase()
{
Services = _container.BeginNewLifetimeScope();
}
public void Dispose()
{
Services.Dispose();
}
[AssemblyInitialize]
public static AssemblyInitializer()
{
var builder = new ContainerBuilder();
builder.RegisterType<ServiceUnderTest>().As<IServiceUnderTest>();
_container = builder.Build();
}
[AssemblyCleanup]
public static AssemblyCleanup()
{
_container?.Dispose();
}
private static _container;
protected ILifetimeScope Services { get; }
}
[TestClass]
public class Tests : IntegrationTestsBase
{
[TestMethod]
public void FirstTest()
{
var svc = Services.Resolve<IServiceUnderTest>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment