Skip to content

Commit 0cd0896

Browse files
committed
Fixed failing acceptance tests and event listener registration to not cause issues with double registration
1 parent 3303383 commit 0cd0896

File tree

8 files changed

+52
-13
lines changed

8 files changed

+52
-13
lines changed

Core.Testing/EventListener.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ public class EventsLog
77
public List<object> PublishedEvents { get; } = new();
88
}
99

10-
public class EventListener<TEvent>: IEventHandler<TEvent>
11-
where TEvent : notnull
10+
public class EventListener: IEventBus
1211
{
12+
private readonly IEventBus eventBus;
1313
private readonly EventsLog eventsLog;
1414

15-
public EventListener(EventsLog eventsLog)
15+
public EventListener(EventsLog eventsLog, IEventBus eventBus)
1616
{
17+
this.eventBus = eventBus;
1718
this.eventsLog = eventsLog;
1819
}
1920

20-
public Task Handle(TEvent @event, CancellationToken cancellationToken)
21+
public async Task Publish(IEventEnvelope eventEnvelope, CancellationToken ct)
2122
{
22-
eventsLog.PublishedEvents.Add(@event);
23-
24-
return Task.CompletedTask;
23+
eventsLog.PublishedEvents.Add(eventEnvelope);
24+
await eventBus.Publish(eventEnvelope, ct);
2525
}
2626
}
27+

Core.Testing/TestWebApplicationFactory.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ protected override IHost CreateHost(IHostBuilder builder)
2222
builder.ConfigureServices(services =>
2323
{
2424
services.AddSingleton(eventsLog)
25-
.AddSingleton(typeof(IEventHandler<>), typeof(EventListener<>))
2625
.AddSingleton<IExternalEventProducer>(externalEventProducer)
2726
.AddSingleton<IEventBus>(sp =>
28-
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
29-
sp.GetRequiredService<IExternalEventProducer>()))
27+
new EventListener(eventsLog,
28+
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
29+
sp.GetRequiredService<IExternalEventProducer>())
30+
)
31+
)
3032
.AddSingleton<IExternalCommandBus>(externalCommandBus)
3133
.AddSingleton<IExternalEventConsumer, DummyExternalEventConsumer>();
3234
});

Sample/EventStoreDB/Simple/ECommerce.Api.Tests/ShoppingCarts/AddingProduct/AddProductTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
using ECommerce.ShoppingCarts.GettingCartById;
44
using FluentAssertions;
55
using Ogooreck.API;
6+
using Warehouse.Api.Tests;
67
using static Ogooreck.API.ApiSpecification;
78
using Xunit;
89

910
namespace Carts.Api.Tests.ShoppingCarts.AddingProduct;
1011

1112
public class AddProductFixture: ApiSpecification<Program>, IAsyncLifetime
1213
{
14+
public AddProductFixture(): base(new ShoppingCartsApplicationFactory()) { }
15+
1316
public Guid ShoppingCartId { get; private set; }
1417

1518
public readonly Guid ClientId = Guid.NewGuid();

Sample/EventStoreDB/Simple/ECommerce.Api.Tests/ShoppingCarts/Canceling/CancelShoppingCartTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
using ECommerce.ShoppingCarts.GettingCartById;
44
using FluentAssertions;
55
using Ogooreck.API;
6+
using Warehouse.Api.Tests;
67
using Xunit;
78
using static Ogooreck.API.ApiSpecification;
89

910
namespace Carts.Api.Tests.ShoppingCarts.Canceling;
1011

1112
public class CancelShoppingCartFixture: ApiSpecification<Program>, IAsyncLifetime
1213
{
14+
public CancelShoppingCartFixture(): base(new ShoppingCartsApplicationFactory()) { }
15+
1316
public Guid ShoppingCartId { get; private set; }
1417

1518
public readonly Guid ClientId = Guid.NewGuid();

Sample/EventStoreDB/Simple/ECommerce.Api.Tests/ShoppingCarts/Confirming/ConfirmShoppingCartTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
using FluentAssertions;
55
using Xunit;
66
using Ogooreck.API;
7+
using Warehouse.Api.Tests;
78
using static Ogooreck.API.ApiSpecification;
89

910
namespace Carts.Api.Tests.ShoppingCarts.Confirming;
1011

1112
public class ConfirmShoppingCartFixture: ApiSpecification<Program>, IAsyncLifetime
1213
{
14+
public ConfirmShoppingCartFixture(): base(new ShoppingCartsApplicationFactory()) { }
15+
1316
public Guid ShoppingCartId { get; private set; }
1417

1518
public readonly Guid ClientId = Guid.NewGuid();

Sample/EventStoreDB/Simple/ECommerce.Api.Tests/ShoppingCarts/Opening/OpenShoppingCartTests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using ECommerce.ShoppingCarts.ProductItems;
66
using FluentAssertions;
77
using Ogooreck.API;
8+
using Warehouse.Api.Tests;
89
using Xunit;
910
using static Ogooreck.API.ApiSpecification;
1011

1112
namespace Carts.Api.Tests.ShoppingCarts.Opening;
1213

13-
public class OpenShoppingCartTests: IClassFixture<TestWebApplicationFactory<Program>>
14+
public class OpenShoppingCartTests: IClassFixture<ShoppingCartsApplicationFactory>
1415
{
1516
private readonly ApiSpecification<Program> API;
1617

@@ -41,8 +42,8 @@ public Task Post_ShouldReturn_CreatedStatus_With_CartId() =>
4142
}))
4243
);
4344

44-
public OpenShoppingCartTests(TestWebApplicationFactory<Program> fixture) =>
45-
API = ApiSpecification<Program>.Setup(fixture);
45+
public OpenShoppingCartTests(ShoppingCartsApplicationFactory applicationFactory) =>
46+
API = ApiSpecification<Program>.Setup(applicationFactory);
4647

4748
public readonly Guid ClientId = Guid.NewGuid();
4849
}

Sample/EventStoreDB/Simple/ECommerce.Api.Tests/ShoppingCarts/RemovingProduct/RemoveProductTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
using FluentAssertions;
55
using Xunit;
66
using Ogooreck.API;
7+
using Warehouse.Api.Tests;
78
using static Ogooreck.API.ApiSpecification;
89

910
namespace Carts.Api.Tests.ShoppingCarts.RemovingProduct;
1011

1112
public class RemoveProductFixture: ApiSpecification<Program>, IAsyncLifetime
1213
{
14+
public RemoveProductFixture(): base(new ShoppingCartsApplicationFactory()) { }
1315
public Guid ShoppingCartId { get; private set; }
1416

1517
public readonly Guid ClientId = Guid.NewGuid();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Core.Testing;
2+
using ECommerce.Storage;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
7+
namespace Warehouse.Api.Tests;
8+
9+
public class ShoppingCartsApplicationFactory: TestWebApplicationFactory<Program>
10+
{
11+
protected override IHost CreateHost(IHostBuilder builder)
12+
{
13+
var host = base.CreateHost(builder);
14+
15+
using var scope = host.Services.CreateScope();
16+
using var context = scope.ServiceProvider.GetRequiredService<ECommerceDbContext>();
17+
var database = context.Database;
18+
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartDetailsProductItem\"");
19+
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartShortInfo\"");
20+
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartDetails\" CASCADE");
21+
22+
return host;
23+
}
24+
}

0 commit comments

Comments
 (0)