TestDataBuilder.AutoFixture 0.1.0

dotnet add package TestDataBuilder.AutoFixture --version 0.1.0                
NuGet\Install-Package TestDataBuilder.AutoFixture -Version 0.1.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="TestDataBuilder.AutoFixture" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestDataBuilder.AutoFixture --version 0.1.0                
#r "nuget: TestDataBuilder.AutoFixture, 0.1.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 TestDataBuilder.AutoFixture as a Cake Addin
#addin nuget:?package=TestDataBuilder.AutoFixture&version=0.1.0

// Install TestDataBuilder.AutoFixture as a Cake Tool
#tool nuget:?package=TestDataBuilder.AutoFixture&version=0.1.0                

TestDataBuilder

A small extension that enables AutoFixture to assign values to public readonly properties and fields.

Usage

TestDataBuilder provides an alternative Builder for AutoFixture that can set values for:

  • Public properties with public getters and setters
  • Public properties with public getters and private setters
  • Public properties with only public getters
  • Public fields
  • Public readonly fields

To create a builder, you can use the following two extension methods for IFixture:

  • To create a builder with default customizations:
var builder = fixture.NewTestDataBuilder<T>();
  • To create a builder with default customizations:
Fixture fixture = new();
var builder = fixture
    .NewTestDataBuilder<T>(
        x => x.Create(y => y.Property, "defaultValue"),
        x => x.Create(y => y.NextProperty, () => 12 * 60)
    );

Adding default customizations at declaration time can be helpful when you need to use the builder to create multiple objects that share some common characteristics.

When a builder is created, it exposes the following methods:

ITestDataBuilder<T> With<TProperty>(Expression<Func<T, TProperty>> expression, TProperty value);
ITestDataBuilder<T> With<TProperty>(Expression<Func<T, TProperty>> expression, Func<TProperty> factory);
ITestDataBuilder<T> Without<TProperty>(Expression<Func<T, TProperty>> expression);
T Create();

For a smoother transition, the syntax is deliberately similar to that used by AutoFixture.

Limitations

At this time, the library cannot:

  • create multiple objects (no equivalent to the CreateMany() method)
  • assign values to readonly properties and fields without an explicit setup using the .With() methods
  • assign with one declaration a value to a readonly field or property that is a nested object

There are simple strategied to overcome those limitations that will be further detailed in the next section of this guide.

How to crete multiple objects that share some common customizations

The simplest way to create multiple objects that share some traits is to store a customized builder with defaults in a variable and then call the .Create() method as often as required. Each instance thus created will be new.

Fixture fixture = new();
var builder = fixture
    .NewTestDataBuilder<MyClass>(
        x => x.Create(y => y.Property, "defaultValue"),
        x => x.Create(y => y.NextProperty, 12)
    );

var input1 = builder.Create();
var input2 = builder.Create();

Assert.Equal(input1.Property, input2.Property); // true
Assert.Equal(input1.NextProperty, input2.NextProperty); // true
Assert.Equal(input1, input2); //false

How to assign a customized value to a readonly property or field that is a reference type

The only way to assign custom values to a property or field that is a reference type is to create a custom builder for that value and pass an instance to the main builder.

class Root
{
    public NestedItem Item { get; }
}

class NestedItem
{
    public int Value { get; }
}

//...

// test definition
Fixture fixture = new();
var nestedItemBuilder = fixture
    .NewTestDataBuilder<NestedItem>(x => x.Create(y => y.Value, 12));

var rootItem = fixture
    .NewTestDataBuilder<Root>()
    .With(x => x.Item, nestedItemBuilder.Create())
    .Create();
Product 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. 
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
0.1.0 105 11/9/2024