Fixture and Create
Learn how Fixture.Create generates anonymous specimens for types you request.
Fixture is the main entry point. Call Create<T>() to get an anonymous instance of T.
Example scenario
You are testing code that needs a ClientDto with a name and age. The exact values do not matter — you only need a populated object for the arrange step.
Example
var fixture = new Fixture();
var number = fixture.Create<int>();
var text = fixture.Create<string>();
var dto = fixture.Create<ClientDto>();
Assert.NotEqual(default, number);
Assert.False(string.IsNullOrEmpty(text));
Assert.False(string.IsNullOrEmpty(dto.Name));
Use CreateMany<T>(count) when you need several values:
var values = fixture.CreateMany<int>(3).ToList();
Assert.Equal(3, values.Count);
Assert.True(values.Distinct().Count() > 1);
How it works
new Fixture()— starts a fresh generator for this testCreate<T>()— resolvesTthrough AutoFixture's specimen pipeline (constructor, properties, nested types)CreateMany<T>(count)— returns an enumerable of distinct anonymous values- Different each call — successive
Create<T>()calls usually return different values, which helps avoid tests that pass only for one hard-coded number - Same value twice — use
FromSeedin Customizations orFreezein Register, Freeze, and Inject when a test needs repeatability or one shared instance
See also Collections.