Moq.AutoMock 4.0.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Moq.AutoMock --version 4.0.2
                    
NuGet\Install-Package Moq.AutoMock -Version 4.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Moq.AutoMock" Version="4.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moq.AutoMock" Version="4.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Moq.AutoMock" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Moq.AutoMock --version 4.0.2
                    
#r "nuget: Moq.AutoMock, 4.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Moq.AutoMock@4.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Moq.AutoMock&version=4.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Moq.AutoMock&version=4.0.2
                    
Install as a Cake Tool

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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (35)

Showing the top 5 NuGet packages that depend on Moq.AutoMock:

Package Downloads
Reo.Core.Testing

Package Description

Reo.Core.IntegrationTesting

Package Description

Reo.Core.CodeGeneratorTesting

Package Description

Relay.Core.Plugins.NUnit

Plugins available for the Core framework

Relay.Web.Testing

A web testing framework built for .NET Core

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.3-ci0882 744 4/21/2026
4.0.3-ci0880 77 4/21/2026
4.0.3-ci0864 1,103 4/3/2026
4.0.2 48,062 4/3/2026
4.0.1 50,558 3/16/2026
4.0.1-ci0862 92 4/3/2026
4.0.1-ci0855 101 3/25/2026
4.0.1-ci0854 95 3/25/2026
4.0.1-ci0852 93 3/25/2026
4.0.1-ci0846 104 3/16/2026
4.0.1-ci0841 148 3/15/2026
4.0.0 4,105 3/15/2026
4.0.0-ci0833 807 3/4/2026
4.0.0-ci0828 855 2/24/2026
4.0.0-ci0823 313 2/20/2026
4.0.0-ci0820 186 2/18/2026
3.6.2-ci0814 6,826 2/16/2026
3.6.2-ci0808 207 2/13/2026
3.6.2-ci0796 412 2/6/2026
3.6.2-ci0793 103 2/6/2026
Loading failed