Need help with Moq.AutoMocker?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

219 Stars 46 Forks MIT License 234 Commits 4 Opened issues


An auto-mocking IoC container for Moq

Services available


Need anything else?

Contributors list

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.


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

var mocker = new AutoMocker();
var car = mocker.CreateInstance();

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

If you have a special instance that you need to use, you can register it with

. This is very similar to registrations in a regular IoC container (i.e.
in StructureMap).
var mocker = new AutoMocker();

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

var car = mocker.CreateInstance();

Extracting Mocks

At some point you might need to get to a mock that was auto-generated. For this, use the

var mocker = new AutoMocker();

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

var car = mocker.CreateInstance(); car.Accelerate(42);

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

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

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

var car = mocker.CreateInstance(); car.Accelerate(42);

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.