Skip to main content
.NETStandard2.0

An auto-mocking container that generates mocks using Moq

Published
Aug 12, 2026
2mo ago
Total Downloads
32.1M
32,140,478

Install

dotnet add package Moq.AutoMock --version 4.0.3-ci0933
ProductVersions
.NET Standard
netstandard2.0

README

Moq.AutoMock Continuous NuGet Status

An automocking container for Moq. Use this if you're invested in your IoC container and want to decouple your unit tests from changes to their constructor arguments.

Usage

Simplest usage is to build an instance that you can unit test.

var mocker = new AutoMocker();
var car = mocker.CreateInstance<Car>();

car.DriveTrain.ShouldNotBeNull();
car.DriveTrain.ShouldImplement<IDriveTrain>();
Mock<IDriveTrain> mock = Mock.Get(car.DriveTrain);

If you have a special instance that you need to use, you can register it with .Use(...). This is very similar to registrations in a regular IoC container (i.e. For<IService>().Use(x) in StructureMap).

var mocker = new AutoMocker();

mocker.Use<IDriveTrain>(new DriveTrain());
// OR, setup a Mock
mocker.Use<IDriveTrain>(x => x.Shaft.Length == 5);

var car = mocker.CreateInstance<Car>();

Extracting Mocks

At some point you might need to get to a mock that was auto-generated. For this, use the .Get<>() or .GetMock<>() methods.

var mocker = new AutoMocker();

// Let's say you have a setup that needs verifying
mocker.Use<IDriveTrain>(x => x.Accelerate(42) == true);

var car = mocker.CreateInstance<Car>();
car.Accelerate(42);

// Then extract & verify
var driveTrainMock = mocker.GetMock<IDriveTrain>();
driveTrainMock.VerifyAll();

Alternately, there's an even faster way to verify all mocks in the container:

var mocker = new AutoMocker();
mocker.Use<IDriveTrain>(x => x.Accelerate(42) == true);

var car = mocker.CreateInstance<Car>();
car.Accelerate(42);

// This method verifies all mocks in the container
mocker.VerifyAll();

HttpClient Support

AutoMocker automatically resolves HttpClient dependencies with a mocked HttpMessageHandler. Use the built-in extension methods to set up responses and verify requests with minimal boilerplate.

var mocker = new AutoMocker();

mocker.SetupHttpGet("/users")
    .ReturnsHttpResponse(HttpStatusCode.OK, """{"users": ["Alice", "Bob"]}""");

var service = mocker.CreateInstance<UserService>();
var response = await service.GetUsersAsync();

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

mocker.VerifyHttpGet("https://example.com/api/users", Times.Once());

Setup methods are available for all common HTTP verbs (SetupHttpGet, SetupHttpPost, SetupHttpPut, SetupHttpDelete, SetupHttpHead) with matching by URL substring or expression predicate. Sequential responses are supported for testing retry logic.

Documentation

For more detailed documentation, including information about the built-in source generators that can automatically generate test boilerplate code, see the docs folder.

Changes from Previous Version

No structural changes between v4.0.3-ci0926 and v4.0.3-ci0933.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

This version is marked prerelease. Validate its API and upgrade path before production use.No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.