Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- A [VerificationGlue](https://ipjohnson.github.io/DependencyModules/guide/testing-verificationglue)
page in the testing guide. VerificationGlue.Tests 0.6.0-preview.1 gives `[VerifyTest]` tests the
services of a module from its attribute, and applies the attributes of
`DependencyModules.Testing` through `ModuleTestComposition`. No DependencyModules package
references VerificationGlue.
- `integ-tests/SutProject.VerificationGlueTests`, which runs SutProject's modules under
VerificationGlue.Tests in the build workflow.

## [1.8.0] - 2026-11-28

This release adds collections and `Func` registrations to the generated code. It also adds
Expand Down
15 changes: 15 additions & 0 deletions DependencyModules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyModules.CodeFixes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyModules.Specs", "tests\DependencyModules.Specs\DependencyModules.Specs.csproj", "{3B3E3C52-B0EB-46DF-98D9-212D60CB68D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SutProject.VerificationGlueTests", "integ-tests\SutProject.VerificationGlueTests\SutProject.VerificationGlueTests.csproj", "{8BCC5D74-F347-4059-B87D-3BA977324D6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -314,6 +316,18 @@ Global
{3B3E3C52-B0EB-46DF-98D9-212D60CB68D4}.Release|x64.Build.0 = Release|Any CPU
{3B3E3C52-B0EB-46DF-98D9-212D60CB68D4}.Release|x86.ActiveCfg = Release|Any CPU
{3B3E3C52-B0EB-46DF-98D9-212D60CB68D4}.Release|x86.Build.0 = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|x64.ActiveCfg = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|x64.Build.0 = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|x86.ActiveCfg = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Debug|x86.Build.0 = Debug|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|Any CPU.Build.0 = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|x64.ActiveCfg = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|x64.Build.0 = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|x86.ActiveCfg = Release|Any CPU
{8BCC5D74-F347-4059-B87D-3BA977324D6B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -341,5 +355,6 @@ Global
{D1AEA1C9-CD76-4664-956B-706962E78E3D} = {FB161494-E422-4D4E-BA04-3473C2EB13B0}
{1233A4FF-C8EE-4422-A26F-EF18D3FFA0F2} = {FB161494-E422-4D4E-BA04-3473C2EB13B0}
{3B3E3C52-B0EB-46DF-98D9-212D60CB68D4} = {F4DC9AA2-7F61-4DEE-A7D1-2BCDBEEFD1C5}
{8BCC5D74-F347-4059-B87D-3BA977324D6B} = {DC7C95FE-F58F-4F16-8DFA-87E1B67DCEAE}
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
</PropertyGroup>

<!--
The VerificationGlue.Tests version that the VerificationGlue.Tests projects under tests/ run
on. It targets net10.0 only. No shipping package references it.
The VerificationGlue.Tests version that the VerificationGlue.Tests projects under tests/ and
integ-tests/ run on. It targets net10.0 only. No shipping package references it.
-->
<PropertyGroup>
<VerificationGlueVersion>0.5.0-preview.1</VerificationGlueVersion>
<VerificationGlueVersion>0.6.0-preview.1</VerificationGlueVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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.
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. VerificationGlue.Tests builds one for each `[VerifyTest]` test from the module attributes.

Documentation: [ipjohnson.github.io/DependencyModules](https://ipjohnson.github.io/DependencyModules/)

Expand Down Expand Up @@ -90,6 +90,7 @@ The test gets its parameters from a new service provider. After the test, the te
- [Decorators](https://ipjohnson.github.io/DependencyModules/guide/decorators) and [interception](https://ipjohnson.github.io/DependencyModules/guide/interception): code around services.
- [Environments](https://ipjohnson.github.io/DependencyModules/guide/environments): registrations that occur only in some environments.
- [Testing](https://ipjohnson.github.io/DependencyModules/guide/testing): xUnit and NUnit tests with modules, mocks, and more service providers in a test.
- [VerificationGlue](https://ipjohnson.github.io/DependencyModules/guide/testing-verificationglue): `[VerifyTest]` tests that get the services of a module from its attribute, with `DependencyModules.Testing` and no adapter package.
- [Native AOT](https://ipjohnson.github.io/DependencyModules/guide/aot): generated factories and trimming.

## Sample projects
Expand All @@ -99,6 +100,7 @@ The `integ-tests` folder contains projects that reference the source projects of
- `SutProject` and `SecondarySutProject`: modules and services.
- `SutProject.Tests`: xUnit tests of these modules.
- `SutProject.NUnitTests`: NUnit tests of these modules.
- `SutProject.VerificationGlueTests`: VerificationGlue.Tests tests of these modules.
- `ConsoleTestProject`: a console application.
- `web/WebApiApp` and `web/WebApiApp.Tests`: an ASP.NET Core application and its tests.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using SecondarySutProject.CircularReferenceModules;

namespace SutProject.VerificationGlueTests.CircularReferenceModules;

public partial class CircularModuleTests
{
[VerifyTest]
[ModuleA]
public void LoadModuleATest(ServiceA serviceA, ServiceB serviceB)
{
Verify(serviceA is not null);
Verify(serviceB is not null);
}

[VerifyTest]
[ModuleB]
public void LoadModuleBTest(ServiceA serviceA, ServiceB serviceB)
{
Verify(serviceA is not null);
Verify(serviceB is not null);
}

[VerifyTest]
[ModuleA]
[ModuleB]
public void LoadModuleBothTest(ServiceA serviceA, ServiceB serviceB)
{
Verify(serviceA is not null);
Verify(serviceB is not null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Reflection;
using DependencyModules.Runtime;
using DependencyModules.Runtime.Attributes;
using DependencyModules.Runtime.Interfaces;

namespace SutProject.VerificationGlueTests.EnvironmentTests;

[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class SeededEnvironmentAttribute(string name) : Attribute, IModuleEnvironmentProvider
{
public IModuleEnvironment? ProvideEnvironment(MethodInfo testMethod) =>
new ModuleEnvironment(false, name);
}

public interface IGatedByEnvironment;

[SingletonService(Realm = typeof(SeededEnvironmentModule))]
[IfEnvironment("seeded-environment")]
public class GatedByEnvironment : IGatedByEnvironment;

[DependencyModule(OnlyRealm = true)]
public partial class SeededEnvironmentModule;

[SeededEnvironmentModule]
public partial class EnvironmentSeedingTests
{
[VerifyTest]
[SeededEnvironment("seeded-environment")]
public void AGatedRegistrationAppliesUnderTheSeededEnvironment(
IServiceProvider provider,
IModuleEnvironment environment
)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is GatedByEnvironment);
Verify(environment.EnvironmentName == "seeded-environment");
}

[VerifyTest]
[SeededEnvironment("other-environment")]
public void TheSameRegistrationIsAbsentUnderAnotherEnvironment(IServiceProvider provider)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is null);
}
}

[SeededEnvironmentModule]
[SeededEnvironment("seeded-environment")]
public partial class EnvironmentSeedingPrecedenceTests
{
[VerifyTest]
public void AClassLevelSeedApplies(IServiceProvider provider)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is GatedByEnvironment);
}

[VerifyTest]
[SeededEnvironment("inner-environment")]
public void TheMethodsSeedBeatsTheClasses(
IServiceProvider provider,
IModuleEnvironment environment
)
{
Verify(provider.GetService(typeof(IGatedByEnvironment)) is null);
Verify(environment.EnvironmentName == "inner-environment");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using DependencyModules.Runtime.Attributes;
using Microsoft.Extensions.DependencyInjection;
using VerificationGlue.Mocks;

namespace SutProject.VerificationGlueTests.KeyedTests;

public interface IKeyedRegistration
{
string Key { get; }
}

[SingletonService(Key = "A", Realm = typeof(KeyedModule))]
public class AKeyedRegistration : IKeyedRegistration
{
public string Key => "A";
}

[SingletonService(Key = "C", Realm = typeof(KeyedModule))]
public class CKeyedRegistration : IKeyedRegistration
{
public string Key => "C";
}

[SingletonService(Realm = typeof(KeyedModule))]
public class CKeyedDependency([FromKeyedServices("C")] IKeyedRegistration registration)
{
public IKeyedRegistration Registration { get; } = registration;
}

[DependencyModule(OnlyRealm = true)]
public partial class KeyedModule;

[KeyedModule]
public partial class KeyedMockTests
{
[VerifyTest]
public void AKeyedMockReachesTheServiceThatInjectsTheKey(
[Mock] [FromKeyedServices("C")] IKeyedRegistration registration,
CKeyedDependency dependency
)
{
registration.Key.Returns("mocked");

Verify(ReferenceEquals(dependency.Registration, registration));
Verify(dependency.Registration.Key == "mocked");
}

[VerifyTest]
public void AKeyedMockLeavesTheOtherKeys(
[Mock] [FromKeyedServices("C")] IKeyedRegistration registration,
[FromKeyedServices("A")] IKeyedRegistration aRegistration
)
{
Verify(aRegistration is AKeyedRegistration);
Verify(registration is not CKeyedRegistration);
}

[VerifyTest]
public void WithoutTheMockTheKeyedServiceIsReal(CKeyedDependency dependency)
{
Verify(dependency.Registration is CKeyedRegistration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DependencyModules.Testing.Attributes;
using VerificationGlue.Mocks;

namespace SutProject.VerificationGlueTests.Mocks;

// Both imported namespaces declare MockAttribute. [Mock] compiles because VerificationGlue.Tests
// adds a global MockAttribute alias to VerificationGlue.Mocks.MockAttribute.
[SutModule]
public partial class MockAliasTests
{
[VerifyTest]
public void MockBindsThroughTheAlias(
IDependencyOne dependencyOne,
[Mock] IScopedService scopedService
)
{
Verify(ReferenceEquals(dependencyOne.ScopedService, scopedService));
}

[VerifyTest]
public void DependencyModulesMockIsReadTheSameWay(
IDependencyOne dependencyOne,
[DependencyModules.Testing.Attributes.Mock] ISingletonService singletonService
)
{
singletonService.GetName().Returns("from DependencyModules' Mock");

Verify(dependencyOne.SingletonService.GetName() == "from DependencyModules' Mock");
}

[VerifyTest]
[TestExport(typeof(IScopedService), Implementation = typeof(ScopedService))]
public void TheOtherTestingAttributesStillResolve(IDependencyOne dependencyOne)
{
Verify(dependencyOne.ScopedService is ScopedService);
}
}
35 changes: 35 additions & 0 deletions integ-tests/SutProject.VerificationGlueTests/Mocks/MockTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using VerificationGlue.Mocks;

namespace SutProject.VerificationGlueTests.Mocks;

[SutModule]
public partial class MockTests
{
[VerifyTest]
public void TheServiceReceivesTheMock(
IDependencyOne dependencyOne,
[Mock] IScopedService scopedService
)
{
Verify(ReferenceEquals(dependencyOne.ScopedService, scopedService));
Verify(scopedService is not ScopedService);
}

[VerifyTest]
public void AMockSetUpWithReturnsAnswersTheService(
[Mock] ISingletonService singletonService,
IDependencyOne dependencyOne
)
{
singletonService.GetName().Returns("mocked");

Verify(dependencyOne.SingletonService.GetName() == "mocked");
}

[VerifyTest]
public void ATestWithoutTheMockGetsTheRealService(IDependencyOne dependencyOne)
{
Verify(dependencyOne.ScopedService is ScopedService);
Verify(dependencyOne.SingletonService is SingletonService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SecondarySutProject.ParameterizedModules;

namespace SutProject.VerificationGlueTests.ParameterizedModuleTests;

public partial class ParameterizedModuleTests
{
[VerifyTest]
[ParameterizedModule("local-string", 20, C = "CValue")]
public void ConstructorArgumentsAndNamedPropertiesReachTheModule(
SomeRuntimeDependency someRuntimeDependency
)
{
Verify(someRuntimeDependency.SomeDependency == "local-string");
Verify(someRuntimeDependency.IntDependency == 20);
Verify(someRuntimeDependency.CValue == "CValue");
}

[VerifyTest]
[SecondaryParameterizedModule]
public void AModuleThatComposesAParameterizedModulePassesItsArguments(
SomeRuntimeDependency someRuntimeDependency
)
{
Verify(someRuntimeDependency.SomeDependency == "test-string");
Verify(someRuntimeDependency.IntDependency == 10);
}
}
Loading
Loading