![]() |
VOOZH | about |
dotnet add package Maersk.Test.AutoFixtureExtensions --version 1.4.0
NuGet\Install-Package Maersk.Test.AutoFixtureExtensions -Version 1.4.0
<PackageReference Include="Maersk.Test.AutoFixtureExtensions" Version="1.4.0" />
<PackageVersion Include="Maersk.Test.AutoFixtureExtensions" Version="1.4.0" />Directory.Packages.props
<PackageReference Include="Maersk.Test.AutoFixtureExtensions" />Project file
paket add Maersk.Test.AutoFixtureExtensions --version 1.4.0
#r "nuget: Maersk.Test.AutoFixtureExtensions, 1.4.0"
#:package Maersk.Test.AutoFixtureExtensions@1.4.0
#addin nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.4.0Install as a Cake Addin
#tool nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.4.0Install as a Cake Tool
This package contains a number of useful extension methods for AutoFixture, making working with AutoFixture simpler.
For basic usage you can instrument your unit test method using the AutoDataWithCustomization attribute like this:
[Theory]
[AutoDataWithCustomization(typeof(AutoMoqCustomization))]
public void Given_some_input_When_mapping_Then_it_maps(
Vessel vessel,
VesselMapper sut)
{
// ...
}
trueIf you need to set ConfigureMembers to true, then do like this instead (this will instruct AutoFixture that members of a mock will be automatically setup to retrieve the return values from the fixture):
[Theory]
[AutoDataWithCustomization(typeof(AutoMoqWithMembersCustomization))]
public void Given_some_input_When_mapping_Then_it_maps(
Vessel vessel,
VesselMapper sut)
{
// ...
}
A common pattern is to move some of the test customization to a separate class. You can do that like this:
[Theory]
[AutoDataWithCustomization(typeof(AutoMoqCustomization), typeof(VesselMapperCustomization))]
public void Given_some_input_When_mapping_Then_it_maps(
Vessel vessel,
VesselMapper sut)
{
// ...
}
private class VesselMapperCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
// ...
}
}
The code placed in the VesselMapperCustomization will get executed before the test.
It can be useful to pass inline parameters to the customization and to the test method - this allows re-using the same test in multiple scenarios.
You can do this using the InlineAutoDataWithCustomization attribute:
[Theory]
[InlineAutoDataWithCustomization(typeof(VesselMapperCustomization), "vessel code 1", "vessel name 1")]
[InlineAutoDataWithCustomization(typeof(VesselMapperCustomization), "vessel code 2", "vessel name 2")]
[InlineAutoDataWithCustomization(typeof(VesselMapperCustomization), "vessel code 3", "vessel name 3")]
public void Given_some_input_and_a_specific_vessel_code_When_mapping_Then_it_maps(
string vesselCode,
string vesselName,
Vessel vessel,
VesselMapper sut)
{
// ...
}
private class VesselMapperCustomization : CompositeCustomization
{
public VesselMapperCustomization(
string vesselCode,
string vesselName)
: base(new AutoMoqCustomization { ConfigureMembers = true }, new AutoMoqWithMembersCustomization())
{
// ...
}
}
vessel code 1 will get assigned to string vesselCode, because it is the first parameter to match the type (string).
Likewise, vessel name 1 will get assigned to string vesselName, because it is the second parameter to match the type (string).
It is of course possible to use different types.
Note that the customization is inheriting from CompositeCustomization, and providing some additional values for that types constructor.
If you don't need to pass the inline parameters to the test method, then use the InlineCustomizationData attribute.
The ParameterNameSpecimenBuilder can be used to set a value for a specific parameter in the test method:
[Theory]
[AutoDataWithCustomization(typeof(AutoMoqCustomization), typeof(VesselMapperCustomization))]
public void Given_some_input_When_mapping_Then_it_maps(
DateTime scheduleArrivalTimeUtc,
Vessel vessel,
VesselMapper sut)
{
// ...
}
private class VesselMapperCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
DateTime scheduleArrivalTimeUtc = // ...
fixture.Customizations.Add(new ParameterNameSpecimenBuilder<DateTimeOffset>(nameof(scheduleArrivalTimeUtc), scheduleArrivalTimeUtc));
}
}
Read the file.
This library was originally written by:
Unit tests and documentation added by:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.