Maersk.Test.AutoFixtureExtensions 1.4.0

Prefix Reserved
dotnet add package Maersk.Test.AutoFixtureExtensions --version 1.4.0                
NuGet\Install-Package Maersk.Test.AutoFixtureExtensions -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Maersk.Test.AutoFixtureExtensions" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Maersk.Test.AutoFixtureExtensions --version 1.4.0                
#r "nuget: Maersk.Test.AutoFixtureExtensions, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Maersk.Test.AutoFixtureExtensions as a Cake Addin
#addin nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.4.0

// Install Maersk.Test.AutoFixtureExtensions as a Cake Tool
#tool nuget:?package=Maersk.Test.AutoFixtureExtensions&version=1.4.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.4.0 172,354 11/17/2023
1.3.1 31,881 9/5/2023
1.2.0 59,863 11/17/2022