Skip to content

Commit

Permalink
Add ApplicationMediatrSupportModuleTests
Browse files Browse the repository at this point in the history
Signed-off-by: Serhii A. Hrytsenko <[email protected]>
  • Loading branch information
gritcsenko committed Jan 26, 2025
1 parent ea8bb84 commit d3981a1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public override async Task AddServicesAsync(IModuleServicesContext context, Canc
serviceConfig.RegisterServicesFromAssemblyContaining(module.GetType());
}

context.Services.AddSingleton(serviceConfig);

ServiceRegistrar.AddMediatRClasses(context.Services, serviceConfig, cancellationToken);
ServiceRegistrar.AddRequiredServices(context.Services, serviceConfig);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Diagnostics.CodeAnalysis;
using HomeInventory.Application.Framework;
using HomeInventory.Modules.Interfaces;
using HomeInventory.Tests.Modules;
using MediatR;
using MediatR.NotificationPublishers;

namespace HomeInventory.Tests.Application;

[SuppressMessage("ReSharper", "UnusedType.Global")]
public sealed class ApplicationMediatrSupportModuleTests() : BaseModuleTest<ApplicationMediatrSupportModule>(static () => new())
{
private readonly SubjectBaseModuleWithMediatr _subject = new();

protected override IReadOnlyCollection<IModule> GetModules() => [_subject];

protected override void EnsureRegistered(IServiceCollection services)
{
_subject.Configured.Should().BeTrue();
services.Should()
.ContainSingleTransient<IMediator>()
.And.ContainSingleTransient<ISender>()
.And.ContainSingleTransient<IPublisher>()
.And.ContainSingleTransient<INotificationPublisher>()
.And.ContainSingleSingleton<MediatRServiceConfiguration>()
.Which.ImplementationInstance.Should().BeOfType<MediatRServiceConfiguration>()
.Which.NotificationPublisherType.Should().Be<TaskWhenAllPublisher>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using HomeInventory.Application.Framework;

namespace HomeInventory.Tests.Application;

public sealed class SubjectBaseModuleWithMediatr : BaseModuleWithMediatr
{
public bool Configured { get; private set; }

public override void Configure(MediatRServiceConfiguration configuration) => Configured = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void ShouldRegisterServices()
.Sut(out var sutVar);

var then = When
.Invoked(sutVar, servicesVar, configurationVar, featureManagerVar, (sut, services, configuration, featureManager) => sut.AddServicesAsync(new ModuleServicesContext(services, configuration, featureManager, [])));
.Invoked(sutVar, servicesVar, configurationVar, featureManagerVar, (sut, services, configuration, featureManager) => sut.AddServicesAsync(new ModuleServicesContext(services, configuration, featureManager, GetModules())));

then
.Ensure(servicesVar, services =>
Expand All @@ -29,6 +29,8 @@ public void ShouldRegisterServices()
}

protected abstract void EnsureRegistered(IServiceCollection services);

protected virtual IReadOnlyCollection<IModule> GetModules() => [];
}

public abstract class BaseModuleTest<TSut>(Func<TSut> createModuleFunc) : BaseModuleTest<FunctionalModuleTestGivenContext<TSut>, TSut>(t => BaseModuleTestGivenContext.Create(t, createModuleFunc))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using HomeInventory.Domain;
using System.Diagnostics.CodeAnalysis;
using HomeInventory.Domain;
using HomeInventory.Domain.Primitives.Ids;
using HomeInventory.Domain.ValueObjects;

namespace HomeInventory.Tests.Modules;

[SuppressMessage("ReSharper", "UnusedType.Global")]
public sealed class DomainModuleTests() : BaseModuleTest<DomainModule>(static () => new())
{
protected override void EnsureRegistered(IServiceCollection services) =>
Expand Down

0 comments on commit d3981a1

Please sign in to comment.