Last active
July 11, 2023 14:30
-
-
Save shoaibshakeel381/6849807f8aa4efc85296b4dce065d603 to your computer and use it in GitHub Desktop.
sample code for using Autofac container for MsTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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