Maersk.Test.AutoFixtureExtensions
1.2.0
Prefix Reserved
See the version list below for details.
dotnet add package Maersk.Test.AutoFixtureExtensions --version 1.2.0
NuGet\Install-Package Maersk.Test.AutoFixtureExtensions -Version 1.2.0
<PackageReference Include="Maersk.Test.AutoFixtureExtensions" Version="1.2.0" />
paket add Maersk.Test.AutoFixtureExtensions --version 1.2.0
#r "nuget: Maersk.Test.AutoFixtureExtensions, 1.2.0"
// Install Maersk.Test.AutoFixtureExtensions as a Cake Addin #addin nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.2.0 // Install Maersk.Test.AutoFixtureExtensions as a Cake Tool #tool nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.2.0
Introduction
This package contains a number of useful extension methods for AutoFixture, making working with AutoFixture simpler.
Examples
Simple scenario
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)
{
// ...
}
ConfigureMembers = true
If 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)
{
// ...
}
Providing a customization
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.
Provide inline parameters to customization
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.
Only pass parameters to the customization
If you don't need to pass the inline parameters to the test method, then use the InlineCustomizationData
attribute.
Specifying a value for a test parameter
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));
}
}
Contribute
Read the CONTRIBUTING.md file.
Changes
Credits
This library was originally written by:
Unit tests and documentation added by:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- AutoFixture (>= 4.17.0)
- AutoFixture.AutoMoq (>= 4.17.0)
- AutoFixture.Xunit2 (>= 4.17.0)
- AutoMapper (>= 11.0.1)
- Castle.Core (>= 4.4.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.