Skip to content

Commit

Permalink
Add test to ensure manual registration overrides the default open imp…
Browse files Browse the repository at this point in the history
…lementation.
  • Loading branch information
zachpainter77 committed Jul 8, 2024
1 parent 8aa2d35 commit ef68751
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/MediatR.Tests/SendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using System.Threading.Tasks;
using Shouldly;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace MediatR.Tests;
public class SendTests
Expand Down Expand Up @@ -48,6 +49,7 @@ public Task<Pong> Handle(Ping request, CancellationToken cancellationToken)
public class Dependency
{
public bool Called { get; set; }
public bool CalledSpecific { get; set; }
}

public class VoidPingHandler : IRequestHandler<VoidPing>
Expand Down Expand Up @@ -103,6 +105,20 @@ public Task Handle(VoidGenericPing<T> request, CancellationToken cancellationTok
}
}


public class TestClass1PingRequestHandler : IRequestHandler<VoidGenericPing<Pong>>
{
private readonly Dependency _dependency;

public TestClass1PingRequestHandler(Dependency dependency) => _dependency = dependency;

public Task Handle(VoidGenericPing<Pong> request, CancellationToken cancellationToken)
{
_dependency.CalledSpecific = true;
return Task.CompletedTask;
}
}

public interface ITestInterface1 { }
public interface ITestInterface2 { }
public interface ITestInterface3 { }
Expand Down Expand Up @@ -222,4 +238,21 @@ public async Task Should_resolve_multiple_type_parameter_generic_handler()

_dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_resolve_closed_handler_if_defined()
{
var dependency = new Dependency();
var services = new ServiceCollection();
services.AddSingleton(dependency);
services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()));
services.AddTransient(typeof(IRequestHandler<VoidGenericPing<Pong>>), typeof(TestClass1PingRequestHandler));
var serviceProvider = services.BuildServiceProvider();
var mediator = serviceProvider.GetService<IMediator>()!;

var request = new VoidGenericPing<Pong>();
await mediator.Send(request);

dependency.CalledSpecific.ShouldBeTrue();
}
}

0 comments on commit ef68751

Please sign in to comment.