Build DSL
Use Build, With, Without, and OmitAutoProperties to control anonymous object creation.
When Create is too coarse, Build<T>() returns a composer you can configure before calling Create().
Example scenario
You are testing a component that reads ClientDto.Name and ClientDto.Age. You need to pin specific values for those properties while AutoFixture fills the rest — or leave some properties at their defaults.
Example
With — set property values:
var fixture = new Fixture();
var expectedName = fixture.Create<string>();
var client = fixture.Build<ClientDto>()
.With(dto => dto.Name, expectedName)
.With(dto => dto.Age, 42)
.Create();
Assert.Equal(expectedName, client.Name);
Assert.Equal(42, client.Age);
Without — skip auto assignment:
var client = fixture.Build<ClientDto>()
.Without(dto => dto.Age)
.Create();
Assert.Equal(default, client.Age);
OmitAutoProperties — empty shell:
var client = fixture.Build<ClientDto>()
.OmitAutoProperties()
.Create();
Assert.Equal(string.Empty, client.Name);
Assert.Equal(default, client.Age);
How it works
Build<T>()— returns anIPostprocessComposer<T>you chain beforeCreate()With— overrides a property value on the final specimenWithout— leaves a property at its default instead of auto-assigningOmitAutoProperties— skips all property assignment; only constructor parameters are filled
Use Build when business rules or test assertions require specific property values. See Refactoring a test for a graph constraint example.