DependencyModules.Testing 1.6.1

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

logo DependencyModules

NuGet build coverage License: MIT

DependencyModules is a source generator for Microsoft.Extensions.DependencyInjection. You put attributes on your classes. When you compile the project, the generator writes the code that registers these classes in an IServiceCollection. The generated code does not use reflection to find services at run time.

A module is a partial class that registers the services of a project. A module can use other modules. The test packages build a service provider from your modules for each xUnit or NUnit test.

Documentation: ipjohnson.github.io/DependencyModules

Packages

Package Contents
DependencyModules.Runtime The attributes, the interfaces, and the AddModule methods.
DependencyModules.SourceGenerator The source generator. It operates only when you compile.
DependencyModules.xUnit [ModuleTest] for xUnit v3.
DependencyModules.NUnit [ModuleTest] and [ModuleTestCase] for NUnit 4.
DependencyModules.Testing The test attributes and interfaces that the xUnit and NUnit packages use.
DependencyModules.NSubstitute [Mock] parameters with NSubstitute.
DependencyModules.Moq [Mock] and Mock<T> parameters with Moq.
DependencyModules.FakeItEasy [Mock] parameters with FakeItEasy.
DependencyModules.SourceGenerator.Impl The source code of the generator. A framework can compile this code into a different source generator.

The target frameworks of the runtime package, DependencyModules.Testing, the test packages, and the mock packages are net8.0 and net10.0. The generator is compatible with Roslyn 4.10 and all subsequent versions.

Install

dotnet add package DependencyModules.Runtime
dotnet add package DependencyModules.SourceGenerator

Register services

Put a service attribute on each class. The attribute sets the lifetime.

using DependencyModules.Runtime.Attributes;

namespace Shop;

public interface IPriceCalculator
{
    decimal Total(decimal price, int quantity);
}

[SingletonService]
public class PriceCalculator : IPriceCalculator
{
    public decimal Total(decimal price, int quantity) => price * quantity;
}

[DependencyModule]
public partial class ShopModule;

The generator registers PriceCalculator as IPriceCalculator. ShopModule registers all services of the project.

Load the module

using DependencyModules.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Shop;

var services = new ServiceCollection();

services.AddModule<ShopModule>();

var provider = services.BuildServiceProvider();

var calculator = provider.GetRequiredService<IPriceCalculator>();

Tests with modules

using DependencyModules.xUnit.Attributes;
using Shop;
using Xunit;

namespace Shop.Tests;

public class PriceCalculatorTests
{
    [ModuleTest(typeof(ShopModule))]
    public void Multiplies(IPriceCalculator calculator)
    {
        Assert.Equal(10m, calculator.Total(2.5m, 4));
    }
}

The test gets its parameters from a new service provider. After the test, the test package disposes the service provider.

Features

  • Services: lifetimes, keyed services, registration types, cross-wired services, and factory methods.
  • Modules: module dependencies, realms, module parameters, and the generated ApplicationModule.
  • Conventions: registrations for all classes that implement an interface.
  • Decorators and interception: code around services.
  • Environments: registrations that occur only in some environments.
  • Testing: xUnit and NUnit tests with modules, mocks, and more service providers in a test.
  • Native AOT: generated factories and trimming.

Sample projects

The integ-tests folder contains projects that reference the source projects of the solution. The solution builds them, and the build workflow runs their tests:

  • SutProject and SecondarySutProject: modules and services.
  • SutProject.Tests: xUnit tests of these modules.
  • SutProject.NUnitTests: NUnit tests of these modules.
  • ConsoleTestProject: a console application.
  • web/WebApiApp and web/WebApiApp.Tests: an ASP.NET Core application and its tests.

License

DependencyModules has the MIT license.

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 (9)

Showing the top 5 NuGet packages that depend on DependencyModules.Testing:

Package Downloads
DependencyModules.xUnit

xUnit v3 support for DependencyModules. [ModuleTest] builds a service provider from your modules for each test and each data row. The test parameters get their values from this service provider.

Hardened.Shared.Testing

Test support for Hardened applications: the entry point attribute that builds a test's container from the real module graph, ITestContext, and the running-test seam a runner package fills. Reference Hardened.Shared.Testing.xUnit or Hardened.Shared.Testing.NUnit beside it for [HardenedTest], and DependencyModules.NSubstitute, DependencyModules.Moq or DependencyModules.FakeItEasy for [Mock].

DependencyModules.Testing.NSubstitute

Package Description

DependencyModules.NSubstitute

NSubstitute mocks for DependencyModules tests. With [NSubstituteSupport], a test parameter with [Mock] gets a substitute. The service provider of the test gives the same substitute to the other services. Use it with DependencyModules.xUnit or DependencyModules.NUnit.

DependencyModules.NUnit

NUnit support for DependencyModules. [ModuleTest] builds a service provider from your modules for each run of a test. The test parameters get their values from this service provider. [ModuleTestCase] gives data rows.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.1 0 9/24/2026
1.6.0 0 9/24/2026
1.5.0 1,151 9/9/2026
1.4.0 185 9/8/2026
1.3.1 605 9/5/2026
1.3.0 471 9/1/2026
1.2.3 174 8/31/2026
1.2.3-preview.1 85 8/29/2026
1.2.2 376 8/29/2026
1.2.1 177 8/28/2026
1.2.0 166 8/28/2026
1.1.0 177 8/26/2026
1.0.0 196 8/16/2026
1.0.0-rc9340 185 8/16/2026
1.0.0-rc9230 1,138 8/12/2026
1.0.0-rc9220 205 8/11/2026
1.0.0-rc9210 156 8/9/2026
1.0.0-RC9040 167 2/1/2025
Loading failed