AutoMoxture.NUnit
2.1.3
See the version list below for details.
dotnet add package AutoMoxture.NUnit --version 2.1.3
NuGet\Install-Package AutoMoxture.NUnit -Version 2.1.3
<PackageReference Include="AutoMoxture.NUnit" Version="2.1.3" />
paket add AutoMoxture.NUnit --version 2.1.3
#r "nuget: AutoMoxture.NUnit, 2.1.3"
// Install AutoMoxture.NUnit as a Cake Addin #addin nuget:?package=AutoMoxture.NUnit&version=2.1.3 // Install AutoMoxture.NUnit as a Cake Tool #tool nuget:?package=AutoMoxture.NUnit&version=2.1.3
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 | 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. |
-
.NETStandard 2.0
- AutoFixture.AutoMoq (>= 4.17.0)
- AutoMoxture.Core (>= 1.0.0)
- NUnit (>= 3.13.3)
- 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 | 265 | 11/12/2023 |
8.0.0 | 113 | 11/9/2023 |
7.3.2 | 140 | 10/23/2023 |
7.3.0 | 187 | 5/31/2023 |
7.2.0 | 174 | 5/28/2023 |
7.1.0 | 156 | 5/27/2023 |
7.0.3 | 152 | 5/26/2023 |
7.0.2 | 157 | 5/26/2023 |
7.0.1 | 152 | 5/26/2023 |
7.0.0 | 159 | 5/26/2023 |
4.0.3 | 166 | 5/26/2023 |
4.0.1 | 149 | 5/26/2023 |
4.0.0 | 164 | 5/26/2023 |
3.1.0 | 183 | 4/25/2023 |
3.0.3 | 188 | 4/18/2023 |
3.0.2 | 195 | 4/18/2023 |
3.0.0 | 178 | 4/18/2023 |
2.2.0 | 178 | 4/18/2023 |
2.1.3 | 193 | 4/18/2023 |
2.1.2 | 198 | 3/29/2023 |
2.1.1 | 212 | 3/29/2023 |
2.1.0 | 219 | 3/29/2023 |
2.0.0 | 264 | 2/7/2023 |
1.1.2 | 495 | 9/23/2022 |
1.1.1 | 479 | 9/23/2022 |
1.1.0 | 437 | 9/22/2022 |
1.0.0 | 457 | 9/22/2022 |