FAQ

Answers to questions people actually ask about AutoFixture.

Why does Create throw ObjectCreationException?

AutoFixture could not finish the object graph. The exception (often ObjectCreationExceptionWithPath) usually means one of these:

CauseWhat to do
Interface or abstract typeAdd AutoMoq, AutoNSubstitute, or AutoFakeItEasy — or Register a concrete type
Circular referenceReplace the default recursion behavior — see below
Property / constructor AutoFixture cannot satisfyUse Build DSL (With / Without / Do) or a customization

Read the request path in the exception — it shows which type failed.

What’s the difference between Freeze, Inject, and Register?

All three change how later requests for a type are resolved:

MethodMeaning
Freeze<T>()AutoFixture creates T once, then reuses that instance
Inject(instance)You supply the instance; every request gets that same object
Register(() => …)You supply a factory; AutoFixture calls it for each request
var frozen = fixture.Freeze<ClientDto>();          // AF creates, then reuses
fixture.Inject(new ClientDto { Name = "x" });      // you own the instance
fixture.Register(() => DateTime.UtcNow.Date);      // factory each time

Register, Freeze, and Inject

How do I deal with circular references?

By default AutoFixture throws when it detects a loop (for example SessionLanguageSessions). That is intentional.

To omit the recursive property instead:

fixture.Behaviors
    .OfType<ThrowingRecursionBehavior>()
    .ToList()
    .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());

For a single object, Build + Without can skip the back-reference without changing fixture-wide behavior.

Circular references

Why can’t AutoFixture create my interface?

AutoFixture builds concrete types. It does not invent implementations for interfaces or abstract classes.

fixture.Customize(new AutoMoqCustomization());
var service = fixture.Create<OrderService>(); // IOrderRepository becomes a Moq mock

Use the same idea with NSubstitute or FakeItEasy. For [AutoData], put the customization in a custom AutoData attribute.

Integrations overview

How do I get mocks into [AutoData] parameters?

Plain [AutoData] has no mocking. Subclass AutoDataAttribute so the fixture includes AutoMoq (or another library), then freeze the mock (or the interface):

public class AutoMoqDataAttribute : AutoDataAttribute
{
    public AutoMoqDataAttribute()
        : base(() => new Fixture().Customize(new AutoMoqCustomization()))
    {
    }
}

[Theory, AutoMoqData]
public void Test(
    [Frozen] Mock<IOrderRepository> repository,
    OrderService sut,
    Order order)
{
    sut.PlaceOrder(order);
    repository.Verify(r => r.Save(order), Times.Once);
}

xUnit.net 3 — custom AutoData · AutoMoq

How do I share fixture setup across many tests?

Don’t repeat new Fixture().Customize(...) in every test. One custom AutoData attribute holds your team conventions (auto-mocking, recursion behavior, date factories, …):

public class DefaultAutoDataAttribute : AutoDataAttribute
{
    public DefaultAutoDataAttribute()
        : base(() => new Fixture()
            .Customize(new AutoMoqCustomization { ConfigureMembers = true }))
    {
    }
}

Use [DefaultAutoData] (and a matching InlineAutoData subclass) on theories.

Custom AutoData attribute

I froze an interface — why isn’t my SUT using it?

Freezing works when the SUT requests the same type you froze (or a matching type with [Frozen(Matching…)]). Library differences:

  • Moq — prefer Freeze<Mock<IOrderRepository>>(). Freezing the interface freezes mock.Object; use Mock.Get(repo) if you need Verify. See AutoMoq wrappers.
  • NSubstitute / FakeItEasy — freeze the interface (there is no Mock<T> wrapper). That instance is what the SUT receives.

With [Frozen] on AutoData, parameter order matters: freeze first, then create the SUT. For a concrete type that should satisfy an interface constructor parameter, use [Frozen(Matching.ImplementedInterfaces)].

Frozen

How do I fill a get-only / read-only collection?

With needs a writable property. For a list exposed with a private setter (or get-only IList<T> filled in the constructor), mutate after create:

var order = fixture.Build<Order>()
    .Do(o => fixture.AddManyTo(o.OrderLines))
    .Create();

Collections

I’m on AutoFixture 4 — what changes in v5?

Pin matching v5 packages together. Test integrations move to AutoFixture.Xunit3 / AutoFixture.NUnit4 and target .NET 8+. Core APIs (Fixture, Create, Build, Customize) stay familiar.

v4 to v5 migration

Is AutoFixture a DI container? Can I use it in production?

No. AutoFixture generates anonymous test data. It is not an application composition root and is not meant for production runtime.

For tests that need interfaces, pair it with a mocking library (AutoMoq, and so on) — still only in the test project.

Generated values are noisy in failure messages — can I make them readable?

Use AutoFixture.SeedExtensions for prefixed strings, or customize how a type is created:

var name = fixture.Create("Customer"); // e.g. "Customer3f2a…"
fixture.Customize<int>(c => c.FromSeed(seed => seed)); // stable ints when seeded

Seed extensions