xunit.frameworks.autofac 0.5.4

dotnet add package xunit.frameworks.autofac --version 0.5.4                
NuGet\Install-Package xunit.frameworks.autofac -Version 0.5.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: xunit.frameworks.autofac, 0.5.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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                

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 49,111 1/30/2022
0.5.2 447 1/30/2022
0.5.1 456 1/30/2022
0.4.0 13,000 2/23/2021
0.3.0 739 2/15/2021
0.1.0 35,472 11/7/2017