DepenMock 1.1.2

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

DepenMock - C# Testing Library

Introduction

DepenMock is a robust C# testing library designed to streamline the mocking process for your System Under Test (SUT) dependencies. By automating the mock creation process, DepenMock simplifies unit testing and encourages adherence to established software design principles.

Setting Up the Test Container

To effectively utilize DepenMock in your unit tests, consider the following base classes that your test classes should inherit from:

BaseTestByAbstraction

This base class is suitable for testing your SUT when it implements an interface. Testing through interfaces helps ensure adherence to the Liskov Substitution Principle (LSP).

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

BaseTestByType

Use this base class when your SUT does not implement an interface, such as when testing an API controller.

public class AccountControllerTests : BaseTestByType<AccountController>
{
    // Your test methods here
}

Creating the System Under Test (SUT)

To create an instance of your SUT, use the ResolveSut method. It is essential to set up any dependencies that need to be mocked before creating the SUT. Place the ResolveSut call as the final step before executing your test action.

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 a list of three items, but you can customize the number of instances by setting by setting the numberOfInstances parameter.

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

// Creates a list of random integers
var randomNumbers = Containers.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 specified data rather than using all generated data.

var deskBookingResult = Container
    .Build<DeskBookingResult>()
    .Without(x => x.DeskBookingId) // Here, 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

Registering Mocked Dependencies

DepenMock's testing framework automatically creates mock dependencies, but you can obtain a reference to a mock dependency to set up methods to return predefined data (stub) or to verify interactions with the dependency (spy).

Creating a stub

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

Creating a spy

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

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

Testing Logging

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

Assert.That(Logger.Logs[LogLevel.Error].TrueForAll(x => x.Contains($"Correlation Id: {correlationId}")));

// Checking different levels of error logs
// Error
Logger.Logs[LogLevel.Error]

// Critical
Logger.Logs[LogLevel.Critical]

// Warning
Logger.Logs[LogLevel.Warning]

// Information
Logger.Logs[LogLevel.Information]

Extending Fixture Customization

DepenMock allows you to add your own customizations to the underlying AutoFixture fixture by overriding a virtual method in your test class. This is useful if you want to add custom ISpecimenBuilder instances or otherwise control how objects are created.

How to Add Custom ISpecimenBuilder Instances

Override the AddContainerCustomizations method in your test class and use the Container.AddCustomizations method to add your custom builders:

public class MyTests : BaseTestByType<MyType>
{
    protected override void AddContainerCustomizations(Container container)
    {
        container.AddCustomizations(new MyCustomSpecimenBuilder());
    }
}

This works for all base test types (BaseTestByType, BaseTestByAbstraction, and their NUnit/XUnit/MSTest variants). The method is called automatically during test setup.

You can add as many custom builders as you need:

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

See the API docs for more details on ISpecimenBuilder and advanced customization scenarios.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on DepenMock:

Package Downloads
DepenMock.NUnit

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

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.

DepenMock.MSTest

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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.2 636 7/25/2025
1.1.1 359 7/25/2025
1.1.0 161 6/21/2025
1.0.2 162 6/16/2025
1.0.1 190 5/31/2025
1.0.0 121 5/31/2025

Initial release