Skip to content

Commit

Permalink
remove Lamar Dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
zachpainter77 committed Apr 18, 2024
1 parent 338482e commit 1552d92
Showing 1 changed file with 12 additions and 117 deletions.
129 changes: 12 additions & 117 deletions test/MediatR.Tests/SendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace MediatR.Tests;
using System;
using System.Threading.Tasks;
using Shouldly;
using Lamar;
using Xunit;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -109,70 +108,24 @@ public Task Handle(VoidGenericPing<T> request, CancellationToken cancellationTok
[Fact]
public async Task Should_resolve_main_handler()
{
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
});
cfg.For<IMediator>().Use<Mediator>();
});

var mediator = container.GetInstance<IMediator>();

var response = await mediator.Send(new Ping { Message = "Ping" });
var response = await _mediator.Send(new Ping { Message = "Ping" });

response.Message.ShouldBe("Ping Pong");
}

[Fact]
public async Task Should_resolve_main_void_handler()
{
var dependency = new Dependency();
await _mediator.Send(new VoidPing());

var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
});
cfg.ForSingletonOf<Dependency>().Use(dependency);
cfg.For<IMediator>().Use<Mediator>();
});

var mediator = container.GetInstance<IMediator>();

await mediator.Send(new VoidPing());

dependency.Called.ShouldBeTrue();
_dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_resolve_main_handler_via_dynamic_dispatch()
{
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
});
cfg.For<IMediator>().Use<Mediator>();
});

var mediator = container.GetInstance<IMediator>();

object request = new Ping { Message = "Ping" };
var response = await mediator.Send(request);
var response = await _mediator.Send(request);

var pong = response.ShouldBeOfType<Pong>();
pong.Message.ShouldBe("Ping Pong");
Expand All @@ -181,95 +134,37 @@ public async Task Should_resolve_main_handler_via_dynamic_dispatch()
[Fact]
public async Task Should_resolve_main_void_handler_via_dynamic_dispatch()
{
var dependency = new Dependency();

var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
});
cfg.ForSingletonOf<Dependency>().Use(dependency);
cfg.For<IMediator>().Use<Mediator>();
});

var mediator = container.GetInstance<IMediator>();

object request = new VoidPing();
var response = await mediator.Send(request);
var response = await _mediator.Send(request);

response.ShouldBeOfType<Unit>();

dependency.Called.ShouldBeTrue();
_dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_resolve_main_handler_by_specific_interface()
{
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<Ping>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
});
cfg.For<ISender>().Use<Mediator>();
});

var mediator = container.GetInstance<ISender>();

var response = await mediator.Send(new Ping { Message = "Ping" });
var response = await _mediator.Send(new Ping { Message = "Ping" });

response.Message.ShouldBe("Ping Pong");
}

[Fact]
public async Task Should_resolve_main_handler_by_given_interface()
{
var dependency = new Dependency();
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<VoidPing>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
});
cfg.ForSingletonOf<Dependency>().Use(dependency);
cfg.For<ISender>().Use<Mediator>();
});

var mediator = container.GetInstance<ISender>();

// wrap requests in an array, so this test won't break on a 'replace with var' refactoring
var requests = new IRequest[] { new VoidPing() };
await mediator.Send(requests[0]);
await _mediator.Send(requests[0]);

dependency.Called.ShouldBeTrue();
_dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_raise_execption_on_null_request()
{
var container = new Container(cfg =>
{
cfg.For<ISender>().Use<Mediator>();
});

var mediator = container.GetInstance<ISender>();

await Should.ThrowAsync<ArgumentNullException>(async () => await mediator.Send(default!));
}
public Task Should_raise_execption_on_null_request() => Should.ThrowAsync<ArgumentNullException>(async () => await _mediator.Send(default!));

[Fact]
public async Task Should_resolve_generic_handler_by_given_interface()
public async Task Should_resolve_generic_handler()
{
var request = new GenericPing<Pong> { Pong = new Pong { Message = "Ping" } };
var result = await _mediator.Send(request);
Expand All @@ -281,7 +176,7 @@ public async Task Should_resolve_generic_handler_by_given_interface()
}

[Fact]
public async Task Should_resolve_generic_void_handler_by_given_interface()
public async Task Should_resolve_generic_void_handler()
{
var request = new VoidGenericPing<Pong>();
await _mediator.Send(request);
Expand Down

0 comments on commit 1552d92

Please sign in to comment.