Specimen graphs

AutoFixture builds nested object graphs automatically from constructor and property dependencies.

When you call Create<T>() on a complex type, AutoFixture fills constructor parameters and properties with anonymous values, including nested types.

Example scenario

You are testing code that reads Person.Name and Person.HomeAddress.City. You need a fully wired object graph without manually constructing Address and assigning it to Person.

Example

var fixture = new Fixture();
var person = fixture.Create<Person>();

Assert.False(string.IsNullOrEmpty(person.Name));
Assert.False(string.IsNullOrEmpty(person.HomeAddress.Street));
Assert.False(string.IsNullOrEmpty(person.HomeAddress.City));

A Person with an Address property gets both objects populated without manual wiring.

How it works

AutoFixture walks constructor parameters and writable properties, creating specimens for each dependency. Nested reference types become part of the same graph in one Create call.

Some graphs have business rules (collections, circular references, interfaces). When Create fails, use Build DSL, Register, Freeze, and Inject, or Circular references.

For how AutoFixture wires builders and specifications inside the kernel, see Specimen builders and specifications.

Next steps