Skip to content

Commit

Permalink
Add a unit test to cover mediating an event with no handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
litenova committed Nov 26, 2023
1 parent 243a2fc commit 956ce1c
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions tests/LiteBus.Events.UnitTests/EventModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LiteBus.Events.UnitTests;
public sealed class EventModuleTests
{
[Fact]
public async Task Mediating_ProductCreatedEvent_ShouldGoThroughHandlersCorrectly()
public async Task mediating_simple_event_goes_through_registered_handlers_correctly()
{
// Arrange
var serviceProvider = new ServiceCollection()
Expand All @@ -38,9 +38,9 @@ public async Task Mediating_ProductCreatedEvent_ShouldGoThroughHandlersCorrectly
@event.ExecutedTypes[6].Should().Be<ProductCreatedEventHandlerPostHandler2>();
@event.ExecutedTypes[7].Should().Be<GlobalEventPostHandler>();
}

[Fact]
public async Task MediatingWithFiltered_ProductCreatedEvent_ShouldGoThroughHandlersCorrectly()
public async Task mediating_ProductCreatedEvent_with_filtered_handlers_goes_through_registered_handlers_correctly()
{
// Arrange
var serviceProvider = new ServiceCollection()
Expand All @@ -51,10 +51,11 @@ public async Task MediatingWithFiltered_ProductCreatedEvent_ShouldGoThroughHandl
var @event = new ProductCreatedEvent();

// Act
await eventMediator.PublishAsync(@event, new EventMediationSettings
{
HandlerFilter = type => type.IsAssignableTo(typeof(IFilteredEventHandler))
});
await eventMediator.PublishAsync(@event,
new EventMediationSettings
{
HandlerFilter = type => type.IsAssignableTo(typeof(IFilteredEventHandler))
});

// Assert
@event.ExecutedTypes.Should().HaveCount(6);
Expand All @@ -66,16 +67,40 @@ public async Task MediatingWithFiltered_ProductCreatedEvent_ShouldGoThroughHandl
@event.ExecutedTypes[4].Should().Be<ProductCreatedEventHandlerPostHandler2>();
@event.ExecutedTypes[5].Should().Be<GlobalEventPostHandler>();
}

[Fact]
public async Task Mediating_ProductViewedEvent_ShouldGoThroughHandlersCorrectly()
public async Task mediating_ProductCreatedEvent_with_no_handler_throw_exception_when_ThrowIfNoHandlerFound_is_set_true()
{
// Arrange
var serviceProvider = new ServiceCollection()
.AddLiteBus(configuration => { configuration.AddEventModule(builder => { builder.RegisterFromAssembly(typeof(ProductCreatedEvent).Assembly); }); })
.BuildServiceProvider();

var eventMediator = serviceProvider.GetRequiredService<IEventMediator>();
var @event = new ProductCreatedEvent();

// Act
var act = () => eventMediator.PublishAsync(@event,
new EventMediationSettings
{
HandlerFilter = _ => false,
ThrowIfNoHandlerFound = true
});

// Assert
await act.Should().ThrowAsync<InvalidOperationException>();
}

[Fact]
public async Task mediating_generic_event_goes_through_registered_handlers_correctly()
{
// Arrange
var serviceProvider = new ServiceCollection()
.AddLiteBus(configuration => { configuration.AddEventModule(builder => { builder.RegisterFromAssembly(typeof(ProductViewedEvent<>).Assembly); }); })
.BuildServiceProvider();

var eventMediator = serviceProvider.GetRequiredService<IEventMediator>();

var @event = new ProductViewedEvent<Mobile>
{
ViewSource = new Mobile()
Expand All @@ -96,7 +121,7 @@ public async Task Mediating_ProductViewedEvent_ShouldGoThroughHandlersCorrectly(
@event.ExecutedTypes[6].Should().Be<ProductViewedEventHandlerPostHandler2<Mobile>>();
@event.ExecutedTypes[7].Should().Be<GlobalEventPostHandler>();
}

[Fact]
public async Task Mediating_ProductUpdatedEvent_ShouldGoThroughHandlersCorrectly()
{
Expand Down

0 comments on commit 956ce1c

Please sign in to comment.