DepenMock.XUnit 1.1.2

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

Sample Project

Explore the sample project, DeskBooker.Core, which includes example unit tests for both NUnit and XUnit 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.2 412 7/25/2025
1.1.1 361 7/25/2025
1.1.0 139 6/21/2025
1.0.2 144 6/16/2025
1.0.1 178 5/31/2025
1.0.0 106 5/31/2025

Initial release