AutoMoxture.XUnit 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoMoxture.XUnit --version 1.1.1
NuGet\Install-Package AutoMoxture.XUnit -Version 1.1.1
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="AutoMoxture.XUnit" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoMoxture.XUnit --version 1.1.1
#r "nuget: AutoMoxture.XUnit, 1.1.1"
#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 AutoMoxture.XUnit as a Cake Addin
#addin nuget:?package=AutoMoxture.XUnit&version=1.1.1

// Install AutoMoxture.XUnit as a Cake Tool
#tool nuget:?package=AutoMoxture.XUnit&version=1.1.1

AutoMoxture

AutoMoxture provides a convenient base test class to work with AutoFixture and AutoMoq so you can inherit all your test classes from it. <br/> <br/>

Back-story

Consider you would like to write unit tests for this class :

public class ServiceWithDependencies
{
    private readonly IDependency1 _dependency1;
    private readonly IDependency2 _dependency2;
    private readonly IDependency3 _dependency3;
    private readonly IDependency4 _dependency4;
    private readonly IDependency5 _dependency5;

    public ServiceWithDependencies(
        IDependency1 dependency1,
        IDependency2 dependency2,
        IDependency3 dependency3,
        IDependency3 dependency4,
        IDependency3 dependency5)
    {
        _dependency1 = dependency1;
        _dependency2 = dependency2;
        _dependency3 = dependency3;
        _dependency4 = dependency4;
        _dependency5 = dependency5;
    }

    public string Concat(string prefix)
    {
        return prefix
            + _dependency1.GetString()
            + _dependency2.GetString()
            + _dependency3.GetString()
            + _dependency4.GetString()
            + _dependency5.GetString();
    }
}

Without AutoMoxture, you would end up writing this at some point :

public class ServiceWithDependenciesTests
{
    ...

    // Arrange
    var mockDependency1 = new Mock<IDependency1>();
    var mockDependency2 = new Mock<IDependency2>();
    var mockDependency3 = new Mock<IDependency3>();
    var mockDependency4 = new Mock<IDependency4>();
    var mockDependency5 = new Mock<IDependency5>();
    var sut = new ServiceWithDependencies(
        mockDependency1.Object,
        mockDependency2.Object,
        mockDependency3.Object,
        mockDependency4.Object,
        mockDependency5.Object);
    
    ...
}

or this :

public class ServiceWithDependenciesTests
{
    private AutoFixture.Fixture Fixture { get; }

    ...

    // Arrange
    var mockDependency1 = Fixture.Create<Mock<IDependency1>>();
    var mockDependency2 = Fixture.Create<Mock<IDependency2>>();
    var mockDependency3 = Fixture.Create<Mock<IDependency3>>();
    var mockDependency4 = Fixture.Create<Mock<IDependency4>>();
    var mockDependency5 = Fixture.Create<Mock<IDependency5>>();
    var sut = new ServiceWithDependencies(
        mockDependency1.Object,
        mockDependency2.Object,
        mockDependency3.Object,
        mockDependency4.Object,
        mockDependency5.Object);

    ...
}

It's quite boring to write and it gets even worse if your class have more dependencies.<br/> Also, if you add and/or remove dependencies, it won't build anymore and you have to go fix your tests. <br/> <br/>

AutoMoxture

Turns out, AutoFixture and AutoMoq have already solved this problem :

var fixture = new AutoFixture.Fixture();
fixture.Customize(new AutoMoqCustomization());
var sut = fixture.Create<ServiceWithDependencies>();

That's more or less what AutoMoxture is doing in its base class so you won't have to write this boilerplate code.

  1. Start by inheriting AutoMoxtureTest :

    public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies>
    {
    }
    
  2. AutoMoxture will create and expose the Sut property out of the box :

    public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies>
    {
        ...
    
        // Act
        var response = Sut.Concat(...);
    
        ...
    }
    
  3. AutoMoxture also inherits from AutoFixture.Fixture so AutoFixture methods are directly accessible :

    public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies>
    {
        ...
    
        // Arrange
        var prefix = Create<string>();
    
        // Act
        var response = Sut.Concat(prefix);
    
        ...
    }
    
  4. AutoMoxture also introduces an alias method Mock<T> to quickly freeze a Mock :

    public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies>
    {
        ...
    
        // Arrange
        var prefix = Create<string>();
    
        var dependentString = Create<string>();
        Mock<Dependency1>()
            .Setup(m => m.GetString())
            .Returns(dependentString);
    
        // Act
        var response = Sut.Concat(prefix);
    
        ...
    }
    
  5. Overall, the test might end up looking like this :

    public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies>
    {
        ...
    
        // Arrange
        var prefix = Create<string>();
    
        var dependentString = Create<string>();
        Mock<Dependency1>()
            .Setup(m => m.GetString())
            .Returns(dependentString);
    
        // Act
        var response = Sut.Concat(prefix);
    
        // Assert
        response.Should().Contain(dependentString);
    
        ...
    }
    

Customize the fixture

It is possible to customize the Fixture in your constructor.

public class ServiceWithDependenciesTests : AutoMoxtureTest
{
    public class ServiceWithDependenciesTests()
    {
        Customize<TheTypeToCustomize>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
    }
}

This will apply to all the tests in the class.<br/> If you don't want this behavior, call the Customize method directly inside your test method.

public class ServiceWithDependenciesTests : AutoMoxtureTest
{
    public void Test1()
    {
        Customize<TheTypeToCustomize>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  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
8.0.1 227 11/12/2023
8.0.0 103 11/9/2023
7.4.1 124 10/23/2023
7.3.0 145 5/31/2023
7.2.0 127 5/28/2023
7.1.0 147 5/27/2023
7.0.3 128 5/26/2023
7.0.2 127 5/26/2023
7.0.1 135 5/26/2023
7.0.0 128 5/26/2023
4.0.3 141 5/26/2023
4.0.1 140 5/26/2023
4.0.0 128 5/26/2023
3.1.0 156 4/25/2023
3.0.3 159 4/18/2023
3.0.2 167 4/18/2023
3.0.0 140 4/18/2023
2.2.0 151 4/18/2023
2.1.3 173 4/18/2023
2.1.2 204 3/29/2023
2.1.1 167 3/29/2023
2.1.0 204 3/29/2023
2.0.0 230 2/7/2023
1.1.2 461 9/23/2022
1.1.1 444 9/23/2022
1.1.0 414 9/22/2022
1.0.0 404 9/22/2022