DepenMock.Moq 4.0.0

dotnet add package DepenMock.Moq --version 4.0.0
                    
NuGet\Install-Package DepenMock.Moq -Version 4.0.0
                    
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="DepenMock.Moq" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DepenMock.Moq" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DepenMock.Moq" />
                    
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 DepenMock.Moq --version 4.0.0
                    
#r "nuget: DepenMock.Moq, 4.0.0"
                    
#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 DepenMock.Moq@4.0.0
                    
#: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=DepenMock.Moq&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DepenMock.Moq&version=4.0.0
                    
Install as a Cake Tool

DepenMock.Moq

Moq integration for DepenMock — a C# testing library that automates mock creation for your System Under Test (SUT) dependencies.

Install this package alongside one of the DepenMock test-framework packages (DepenMock.XUnit, DepenMock.NUnit, or DepenMock.MSTest) to use Moq as your mocking framework.

Installation

dotnet add package DepenMock.Moq
dotnet add package DepenMock.XUnit   # or DepenMock.NUnit / DepenMock.MSTest

Setting Up the Test Container

The base classes require an IMockFactory to be passed through the constructor. The recommended approach is to define a project-level base class once that wires up MoqMockFactory, so individual test classes need no constructor boilerplate.

Recommended: project-level base class

Define these once in your test project and inherit from them everywhere:

public abstract class TestByAbstraction<TType, TInterface>
    : BaseTestByAbstraction<TType, TInterface>(new MoqMockFactory())
    where TType : class, TInterface;

public abstract class TestByType<TType>
    : BaseTestByType<TType>(new MoqMockFactory())
    where TType : class;

Then each test class simply inherits with no constructor required:

public class DeskBookingRequestProcessorTests
    : TestByAbstraction<DeskBookingRequestProcessor, IDeskBookingRequestProcessor>
{
    // Your test methods here
}

Alternative: pass the factory directly

If you prefer not to define a shared base class, pass the factory explicitly per test class:

public class DeskBookingRequestProcessorTests
    : BaseTestByAbstraction<DeskBookingRequestProcessor, IDeskBookingRequestProcessor>
{
    public DeskBookingRequestProcessorTests()
        : base(new MoqMockFactory()) { }
}

public class AccountControllerTests : BaseTestByType<AccountController>
{
    public AccountControllerTests()
        : base(new MoqMockFactory()) { }
}

Creating the System Under Test (SUT)

Call ResolveSut() as the final step of your arrange phase, after any mock setup.

var sut = ResolveSut();

Creating Mock Data

Create<T>()

The Create<T> method generates a single instance of the specified type.

// Creates a random string in the format of a guid i.e "fe06998d-aec1-4808-8968-d8f37024a294"
var name = Container.Create<string>();

// Creates a random integer greater than 0
var id = Container.Create<int>();

// Creates a boolean value set to true
var isEnabled = Container.Create<bool>();

// Creates a random date set in the future
var startDate = Container.Create<DateTime>();

// Creates an instance of the class MyObj
var myObjInstance = Container.Create<MyObj>();

// Creates a list of strings. NOTE: Use CreateMany for generating lists.
var randomStrings = Container.Create<List<string>>();

CreateMany<T>(int? numberOfInstances)

The CreateMany<T> method generates a list of instances of the specified type. By default it creates three items, but you can customise the count with the numberOfInstances parameter.

// Creates a list of strings
var randomStrings = Container.CreateMany<string>();

// Creates a list of random integers
var randomNumbers = Container.CreateMany<int>();

// Creates a list of boolean values. All values will be set to true.
var allTrueValues = Container.CreateMany<bool>();

// Creates a list of random dates. This example creates a list of 5 random dates.
var randomDates = Container.CreateMany<DateTime>(5);

// Creates a list of addresses set to random values
var addressList = Container.CreateMany<Address>();

Build<T>()

The Build<T> method provides a builder pattern for creating objects with specific values rather than using all auto-generated data.

var deskBookingResult = Container
    .Build<DeskBookingResult>()
    .Without(x => x.DeskBookingId) // DeskBookingId will be set to null
    .With(x => x.Code, DeskBookingResultCode.Success)
    .With(x => x.FirstName, request.FirstName)
    .With(x => x.LastName, request.LastName)
    .With(x => x.Email, request.Email)
    .With(x => x.Date, request.Date)
    .Create();

Creating Mock Dependencies

DepenMock automatically creates Moq mocks for all unregistered dependencies. Call ResolveMock<T>() to get a reference to a mock, then use AsMoq() to access Moq's Setup and Verify APIs.

Creating a stub

Container
    .ResolveMock<IDeskRepository>()
    .AsMoq()
    .Setup(x => x.GetAvailableDesks(It.IsAny<DateTime>()))
    .Returns(Container.CreateMany<Desk>());

Creating a spy

var mockRepo = Container.ResolveMock<IDeskBookingRepository>().AsMoq();

// ... act ...

mockRepo.Verify(x => x.Save(It.IsAny<DeskBooking>()), Times.Once);

Testing Logging

DepenMock provides a ListLogger and automatically injects it as a dependency for your tests.

Logger.ErrorLogs().AssertContains($"Correlation Id: {correlationId}");

// Accessing logs by level directly
Logger.Logs[LogLevel.Error]
Logger.Logs[LogLevel.Critical]
Logger.Logs[LogLevel.Warning]
Logger.Logs[LogLevel.Information]

Extending Fixture Customization

Override AddContainerCustomizations to register custom ISpecimenBuilder instances or otherwise control how objects are created.

public class MyTests : BaseTestByType<MyType>
{
    public MyTests() : base(new MoqMockFactory()) { }

    protected override void AddContainerCustomizations(Container container)
    {
        container.AddCustomizations(new MyCustomSpecimenBuilder());
    }
}

You can add as many custom builders as you need:

container.AddCustomizations(new MyBuilder1(), new MyBuilder2());

Sample Project

Explore the sample project, DeskBooker.Core, which includes example unit tests for NUnit, XUnit, and MSTest to help you get started with DepenMock.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DepenMock.Moq:

Package Downloads
DepenMock.XUnit

XUnit testing library that can automatically mock your SUT (System Under Test) dependencies. This package provides XUnit integration for DepenMock, making it easy to use auto-mocking in your XUnit tests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.0 156 6/28/2026
3.1.0 162 4/26/2026
3.0.0 141 4/19/2026
2.0.0 185 4/18/2026