Skip to content

Repository files navigation

Build Status Nuget Package

About the project

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.

How to use

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()
	{
	}
}

License

MIT

About

No description, website, or topics provided.

Resources

Stars

8 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages