AutoMoxture.XUnit
2.2.0
See the version list below for details.
dotnet add package AutoMoxture.XUnit --version 2.2.0
NuGet\Install-Package AutoMoxture.XUnit -Version 2.2.0
<PackageReference Include="AutoMoxture.XUnit" Version="2.2.0" />
<PackageVersion Include="AutoMoxture.XUnit" Version="2.2.0" />
<PackageReference Include="AutoMoxture.XUnit" />
paket add AutoMoxture.XUnit --version 2.2.0
#r "nuget: AutoMoxture.XUnit, 2.2.0"
#:package AutoMoxture.XUnit@2.2.0
#addin nuget:?package=AutoMoxture.XUnit&version=2.2.0
#tool nuget:?package=AutoMoxture.XUnit&version=2.2.0
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
Sutproperty 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 | 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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. |
-
.NETStandard 2.0
- AutoFixture.AutoMoq (>= 4.17.0)
- AutoMoxture.Core (>= 1.0.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 | 334 | 11/12/2023 |
| 8.0.0 | 175 | 11/9/2023 |
| 7.4.1 | 225 | 10/23/2023 |
| 7.3.0 | 258 | 5/31/2023 |
| 7.2.0 | 260 | 5/28/2023 |
| 7.1.0 | 253 | 5/27/2023 |
| 7.0.3 | 250 | 5/26/2023 |
| 7.0.2 | 260 | 5/26/2023 |
| 7.0.1 | 263 | 5/26/2023 |
| 7.0.0 | 241 | 5/26/2023 |
| 4.0.3 | 261 | 5/26/2023 |
| 4.0.1 | 243 | 5/26/2023 |
| 4.0.0 | 245 | 5/26/2023 |
| 3.1.0 | 264 | 4/25/2023 |
| 3.0.3 | 291 | 4/18/2023 |
| 3.0.2 | 317 | 4/18/2023 |
| 3.0.0 | 268 | 4/18/2023 |
| 2.2.0 | 281 | 4/18/2023 |
| 2.1.3 | 268 | 4/18/2023 |
| 2.1.2 | 315 | 3/29/2023 |
| 2.1.1 | 316 | 3/29/2023 |
| 2.1.0 | 327 | 3/29/2023 |
| 2.0.0 | 373 | 2/7/2023 |
| 1.1.2 | 617 | 9/23/2022 |
| 1.1.1 | 596 | 9/23/2022 |
| 1.1.0 | 566 | 9/22/2022 |
| 1.0.0 | 536 | 9/22/2022 |