This project contains an integration between xUnit and Autofac. It adds the support for the dependency injection (DI) into all xUnit entities such as test classes, fixtures, etc.
Install the NuGet package.
Install-Package xunit.autofac
In your testing project, add the following attribute to the AssemblyInfo.cs
file.
[assembly: Xunit.Autofac.UseAutofacTestFramework]
Register your dependencies by adding Autofac modules to your testing project.
public class ServiceRegistration : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(context => new MyService())
.As<IMyService>()
.InstancePerLifetimeScope();
}
}
After that, you can use dependency injection in the test entities in your code.
public class TestClass
{
private readonly IMyService _myService;
public TestClass(IMyService myService)
{
_myService = myService; // this dependency will be injected automatically by Autofac
}
[Fact]
public void Test()
{
}
}