xunit.frameworks.autofac 0.5.4

.NET 6.0 .NET Standard 2.0 .NET Framework 4.8
Install-Package xunit.frameworks.autofac -Version 0.5.4
dotnet add package xunit.frameworks.autofac --version 0.5.4
<PackageReference Include="xunit.frameworks.autofac" Version="0.5.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add xunit.frameworks.autofac --version 0.5.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: xunit.frameworks.autofac, 0.5.4"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install xunit.frameworks.autofac as a Cake Addin
#addin nuget:?package=xunit.frameworks.autofac&version=0.5.4

// Install xunit.frameworks.autofac as a Cake Tool
#tool nuget:?package=xunit.frameworks.autofac&version=0.5.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

xUnit Autofac

Use Autofac to resolve xUnit test cases.

The Test runners and discoverers are based on their xUnit counterparts. If [UseAutofacTestFramework] is missing, the tests in that class are run by the normal xUnit runners.

Originally a fork of xunit.ioc.autofac by @dennisroche

How to use

Install the Nuget package.

Install-Package xunit.frameworks.autofac

In your testing project, add the following framework

[assembly: TestFramework("Your.Test.Project.ConfigureTestFramework", "AssemblyName")]

namespace Your.Test.Project
{
    public class ConfigureTestFramework : AutofacTestFramework
    {
        public ConfigureTestFramework(IMessageSink diagnosticMessageSink)
            : base(diagnosticMessageSink)
        {
        }

        protected override void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterType<CurrentTestInfo>().AsSelf().InstancePerTest();
            builder.RegisterType<CurrentTheoryInfo>().AsSelf().InstancePerTheory();
            builder.RegisterType<CurrentTestClassInfo>().AsSelf().InstancePerTestClass();
            builder.RegisterType<CurrentTestCollectionInfo>().AsSelf().InstancePerTestCollection();

            builder.RegisterSource(new NSubstituteRegistrationSource()); // https://gist.github.com/dabide/57c5279894383d8f0ee4ed2069773907

            builder.RegisterType<Foo>().As<IFoo>();

            // configure your container
            // e.g. builder.RegisterModule<TestOverrideModule>();
        }
    }
}

Example test class.

[UseAutofacTestFramework] // Without this attribute, the test class will be handled by the standard xUnit test runners
public class MyAwesomeTests
{
    public MyAwesomeTests(IFoo foo)
    {
        _foo = foo;
    }

    [Fact]
    public void AssertThatWeDoStuff()
    {
        Console.WriteLine(_foo.Bar);
    }

    private readonly ITestOutputHelper _outputHelper;
}

public interface IFoo
{
    Guid Bar { get; }
}

public class Foo : IFoo
{
    public Guid Bar { get; } = Guid.NewGuid();
}

ICollectionFixture<T>, IClassFixture<T>, and ITheoryFixture<T> are also supported, together with INeedModule<T>. (The latter specifies Autofac modules to be loaded when the lifetime scope is created.) This enables very elegant solutions:

[UseAutofacTestFramework]
public class MyEvenMoreAwesomeTests : IUseInMemoryDb
{
    public MyEvenMoreAwesomeTests(IDbConnectionFactory dbConnectionFactory)
    {
        _dbConnectionFactory = dbConnectionFactory;
    }

    [Fact]
    public void AssertThatWeDoEvenMoreStuff()
    {
        using (IDbConnection db = _dbConnectionFactory.Open())
        {
            db.CreateTableIfNotExists<Foo>();
            // ... and so on
        }
    }

    private readonly IDbConnectionFactory _dbConnectionFactory;
}

public interface IUseInMemoryDb : IClassFixture<MemoryDatabaseClassFixture>
{
}

public class MemoryDatabaseClassFixture : IDisposable, INeedModule<MemoryDatabaseClassFixture.MemoryDatabaseFixtureModule>
{
    private readonly IDbConnection _db;

    public MemoryDatabaseClassFixture(IDbConnectionFactory dbConnectionFactory)
    {
        // Keep the in-memory database alive
        _db = dbConnectionFactory.Open();
    }

    public void Dispose()
    {
        // Now it can rest in peace
        _db?.Dispose();
    }

    public class MemoryDatabaseFixtureModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(c => new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider)).As<IDbConnectionFactory>().SingleInstance();
        }
    }
}

License

MIT

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.4 1,756 1/30/2022
0.5.2 90 1/30/2022
0.5.1 92 1/30/2022
0.4.0 5,543 2/23/2021
0.3.0 252 2/15/2021
0.1.0 26,248 11/7/2017