AutoMoq

Moq integration — let AutoFixture create Moq mocks for interfaces and abstract types automatically.

Moq is a .NET mocking library. You create Mock<T> instances for interfaces and abstract types, then set up return values and verify calls.

AutoFixture.AutoMoq plugs Moq into AutoFixture. When AutoFixture builds an object graph and needs an interface or abstract dependency, AutoMoq supplies a Moq mock instead of failing — so you can Freeze mocks and assert on them with less manual setup.

Prerequisites

Install

Install AutoFixture.AutoMoq and add an explicit Moq package reference in the same test project.

AutoMoq depends on Moq, but that dependency version stays intentionally low so AutoMoq remains compatible with a wide range of Moq releases. If you rely only on the transitive Moq reference, you may get an older Moq without the latest features and fixes. Reference the latest Moq yourself alongside AutoMoq.

<PackageReference Include="AutoFixture" Version="5.0.0-rc.1" />
<PackageReference Include="AutoFixture.AutoMoq" Version="5.0.0-rc.1" />
<PackageReference Include="Moq" Version="4.20.72" />

Pin matching versions of AutoFixture and AutoFixture.AutoMoq. Do not mix v4 and v5 AutoFixture packages in the same project. Keep your explicit Moq reference on the newest available version when you update packages.

Example scenario

You are testing OrderService, which depends on IOrderRepository. The test should verify that placing an order calls Save on the repository — without hand-writing a mock setup for every test. Other tests need mock methods to return fixture values, or Moq-backed delegates.

Basic usage

var fixture = new Fixture().Customize(new AutoMoqCustomization());
var repository = fixture.Freeze<Mock<IOrderRepository>>();

var service = fixture.Create<OrderService>();

var order = fixture.Create<Order>();
service.PlaceOrder(order);

repository.Verify(r => r.Save(order), Times.Once);

Create<OrderService>() injects a mock IOrderRepository. Freeze<Mock<IOrderRepository>>() keeps one mock instance so you can verify calls.

Combine with xUnit.net 3 via a custom AutoData attribute when tests use [AutoData].

Mock wrappers and interface requests

AutoMoq registers builders for both Mock<T> and the mocked type T (interfaces and abstract classes). Prefer freezing the mock when you need Setup / Verify:

var fixture = new Fixture().Customize(new AutoMoqCustomization());
var repository = fixture.Freeze<Mock<IOrderRepository>>();

var service = fixture.Create<OrderService>();

Freezing the interface works too — the relay returns mock.Object, and you can recover the mock with Mock.Get:

var fixture = new Fixture().Customize(new AutoMoqCustomization());
var repository = fixture.Freeze<IOrderRepository>();

var service = fixture.Create<OrderService>();
var order = fixture.Create<Order>();
service.PlaceOrder(order);

Mock.Get(repository).Verify(r => r.Save(order), Times.Once);

Create<Mock<T>>() builds a configured Mock<T> when you want the wrapper without freezing.

Configuration options

AutoMoqCustomization exposes properties you set before calling Customize. Defaults are all off / default relay:

PropertyDefaultEffect
ConfigureMembersfalseWhen true, mock members return values from the fixture
GenerateDelegatesfalseWhen true, Moq creates delegates (Func, Action, …)
RelayMockRelayResidue collector that turns interface/abstract requests into mocks
var fixture = new Fixture().Customize(new AutoMoqCustomization
{
    ConfigureMembers = true,
    GenerateDelegates = true
});

ConfigureMembers

With the default (ConfigureMembers = false), Moq uses its usual loose defaults — methods return default unless you set them up:

public interface IGreeter
{
    string Greet();
}
var fixture = new Fixture().Customize(new AutoMoqCustomization());

var greeter = fixture.Create<IGreeter>();

Assert.Null(greeter.Greet());

With ConfigureMembers = true, AutoFixture sets up mock members so calls return specimens from the fixture (and stubs overridable properties like real properties):

var fixture = new Fixture().Customize(new AutoMoqCustomization
{
    ConfigureMembers = true
});
var order = fixture.Freeze<Order>();

var repository = fixture.Create<ICommerceOrderRepository>();

Assert.Same(order, repository.GetById(1));

Use ConfigureMembers when the SUT reads from collaborators and you want anonymous return values without writing Setup for every member. Prefer the default when you only care about verifying calls (as in the basic Verify example).

The older AutoConfiguredMoqCustomization type is obsolete — use new AutoMoqCustomization { ConfigureMembers = true } instead.

ConfigureMembers does not set up every member. AutoMoq skips:

  • Generic methods (Moq cannot anticipate type arguments)
  • Methods with ref parameters
  • Sealed methods
  • Void methods without out parameters

For those cases, set up the mock yourself — for example with ReturnsUsingFixture.

ReturnsUsingFixture

ReturnsUsingFixture is a Moq Setup extension that returns the next specimen of the member's return type from the fixture. Use it for members ConfigureMembers cannot cover (especially generics):

public interface IConverter
{
    T Convert<T>(string value);
}
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var converter = fixture.Freeze<Mock<IConverter>>();
var expected = fixture.Freeze<double>();

converter.Setup(c => c.Convert<double>("10.0")).ReturnsUsingFixture(fixture);

Assert.Equal(expected, converter.Object.Convert<double>("10.0"));

GenerateDelegates

By default, AutoFixture's own kernel creates delegates. With GenerateDelegates = true, Moq creates them instead — useful when you want mockable or auto-configured delegates:

var fixture = new Fixture().Customize(new AutoMoqCustomization
{
    GenerateDelegates = true,
    ConfigureMembers = true
});
var expected = fixture.Freeze<string>();

var format = fixture.Create<Func<int, string>>();

Assert.Equal(expected, format(42));

Without ConfigureMembers, the Moq delegate still exists but return values stay at Moq defaults unless you set them up.

Relay

Relay is the specimen builder added to fixture.ResidueCollectors. The default MockRelay answers requests for interfaces and abstract types with Moq mocks.

Most tests leave the default. Replace it only when you need a custom residue collector — for example a MockRelay with a different request specification:

var fixture = new Fixture().Customize(new AutoMoqCustomization
{
    Relay = new MockRelay(new ExactTypeSpecification(typeof(IOrderRepository)))
});

That example would mock only IOrderRepository requests that match the specification you supply; other abstractions follow the rest of the fixture pipeline.

How it works

  • AutoMoqCustomization — registers Moq mock creation and a residue collector for abstractions
  • ConfigureMembers — auto-setup mock members from the fixture (with known gaps — see above)
  • ReturnsUsingFixture — manual Moq setup that pulls return values from the fixture
  • GenerateDelegates — create delegates with Moq instead of the AutoFixture kernel
  • Relay — residue collector (default MockRelay) for interface/abstract requests
  • Freeze<Mock<T>>() / Freeze<T>() — one shared mock (or mock.Object) for setup and verification

Next steps

API