Register, Freeze, and Inject
Three ways to supply known instances or factories to a fixture.
Use Inject when you already have the object. Use Freeze when AutoFixture should create it once and reuse it. Use Register when you control how each request is built.
Example scenario
You are testing code that resolves the same ClientDto from multiple places in an object graph. You need one shared instance — or a factory that always returns a known string.
Example
Register — factory for a type:
var fixture = new Fixture();
fixture.Register(() => "registered");
Assert.Equal("registered", fixture.Create<string>());
Freeze — one shared instance:
var frozen = fixture.Freeze<ClientDto>();
var resolved = fixture.Create<ClientDto>();
Assert.Same(frozen, resolved);
Inject — exact instance:
var injected = new ClientDto { Name = "injected", Age = 1 };
fixture.Inject(injected);
Assert.Same(injected, fixture.Create<ClientDto>());
How it works
Register<T>(Func<T>)— replaces howTis created on every requestFreeze<T>()— createsTonce, then injects that instance so later requests on this fixture reuse itInject<T>(T)— supplies a specific instance for all later requests of that type on this fixture
See Customizations.