AutoMoxture.XUnit
7.4.1
See the version list below for details.
dotnet add package AutoMoxture.XUnit --version 7.4.1
NuGet\Install-Package AutoMoxture.XUnit -Version 7.4.1
<PackageReference Include="AutoMoxture.XUnit" Version="7.4.1" />
paket add AutoMoxture.XUnit --version 7.4.1
#r "nuget: AutoMoxture.XUnit, 7.4.1"
// Install AutoMoxture.XUnit as a Cake Addin #addin nuget:?package=AutoMoxture.XUnit&version=7.4.1 // Install AutoMoxture.XUnit as a Cake Tool #tool nuget:?package=AutoMoxture.XUnit&version=7.4.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.
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)
{
this.dependency1 = dependency1;
this.dependency2 = dependency2;
this.dependency3 = dependency3;
this.dependency4 = dependency4;
this.dependency5 = dependency5;
}
public string Concat(string prefix)
{
return prefix
+ this.dependency1.GetString()
+ this.dependency2.GetString()
+ this.dependency3.GetString()
+ this.dependency4.GetString()
+ this.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 = this.Fixture.Create<Mock<IDependency1>>();
var mockDependency2 = this.Fixture.Create<Mock<IDependency2>>();
var mockDependency3 = this.Fixture.Create<Mock<IDependency3>>();
var mockDependency4 = this.Fixture.Create<Mock<IDependency4>>();
var mockDependency5 = this.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.
Also, if you add and/or remove dependencies, it won't build anymore and you have to go fix your tests.
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.
Start by inheriting AutoMoxtureTest :
public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies> { }
AutoMoxture will create and expose the
Sut
property out of the box :public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies> { ... // Act var response = this.Sut.Concat(...); ... }
AutoMoxture also exposes an instance of AutoFixture so regular/usual AutoFixture methods are accessible :
public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies> { ... // Arrange var prefix = this.Fixture.Create<string>(); // Act var response = this.Sut.Concat(prefix); ... }
AutoMoxture also introduces an alias method
Mock<T>
to quickly freeze a Mock :public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies> { ... // Arrange var prefix = this.Fixture.Create<string>(); var dependentString = this.Fixture.Create<string>(); this.Fixture.Mock<Dependency1>() .Setup(m => m.GetString()) .Returns(dependentString); // Act var response = this.Sut.Concat(prefix); ... }
Overall, the test might end up looking like this :
public class ServiceWithDependenciesTests : AutoMoxtureTest<ServiceWithDependencies> { ... // Arrange var prefix = this.Fixture.Create<string>(); var dependentString = this.Fixture.Create<string>(); this.Fixture.Mock<Dependency1>() .Setup(m => m.GetString()) .Returns(dependentString); // Act var response = this.Fixture.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()
{
this.Fixture.Customize<TheTypeToCustomize>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
}
}
This will apply to all the tests in the class.
If you don't want this behavior, call the Customize method directly inside your test method.
public class ServiceWithDependenciesTests : AutoMoxtureTest
{
public void Test1()
{
this.Fixture.Customize<TheTypeToCustomize>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
}
}
Customize the SUT factory
Sometimes you need to control the way the SUT is created. More likely, you may have a single test that needs a particular SUT setup. You can provide a new factory at any time like this:
// Do stuff with the old/regular SUT
var sutBeforeChange = this.Sut;
// Provide a new factory
this.SutFactory = () => new TheSutType();
// Do stuff with the new/latest SUT
var sutAfterChange = this.Sut;
Note that the SUT will still keep its value after being reassigned:
this.SutFactory = () => new TheSutType();
var sutAfterChange1 = this.Sut;
var sutAfterChange2 = this.Sut;
// Then sutAfterChange1 == sutAfterChange2
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.0
- AutoFixture.Xunit2 (>= 4.18.0)
- System.ComponentModel.TypeConverter (>= 4.3.0)
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 | 262 | 11/12/2023 |
8.0.0 | 125 | 11/9/2023 |
7.4.1 | 149 | 10/23/2023 |
7.3.0 | 168 | 5/31/2023 |
7.2.0 | 152 | 5/28/2023 |
7.1.0 | 173 | 5/27/2023 |
7.0.3 | 159 | 5/26/2023 |
7.0.2 | 154 | 5/26/2023 |
7.0.1 | 158 | 5/26/2023 |
7.0.0 | 149 | 5/26/2023 |
4.0.3 | 166 | 5/26/2023 |
4.0.1 | 162 | 5/26/2023 |
4.0.0 | 155 | 5/26/2023 |
3.1.0 | 179 | 4/25/2023 |
3.0.3 | 182 | 4/18/2023 |
3.0.2 | 189 | 4/18/2023 |
3.0.0 | 164 | 4/18/2023 |
2.2.0 | 175 | 4/18/2023 |
2.1.3 | 196 | 4/18/2023 |
2.1.2 | 225 | 3/29/2023 |
2.1.1 | 192 | 3/29/2023 |
2.1.0 | 227 | 3/29/2023 |
2.0.0 | 253 | 2/7/2023 |
1.1.2 | 488 | 9/23/2022 |
1.1.1 | 473 | 9/23/2022 |
1.1.0 | 437 | 9/22/2022 |
1.0.0 | 428 | 9/22/2022 |