Skip to content

Commit

Permalink
Fixed failing acceptance tests and event listener registration to not…
Browse files Browse the repository at this point in the history
… cause issues with double registration
  • Loading branch information
oskardudycz committed Jun 16, 2022
1 parent 3303383 commit 0cd0896
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 13 deletions.
15 changes: 8 additions & 7 deletions Core.Testing/EventListener.cs
Expand Up @@ -7,20 +7,21 @@ public class EventsLog
public List<object> PublishedEvents { get; } = new();
}

public class EventListener<TEvent>: IEventHandler<TEvent>
where TEvent : notnull
public class EventListener: IEventBus
{
private readonly IEventBus eventBus;
private readonly EventsLog eventsLog;

public EventListener(EventsLog eventsLog)
public EventListener(EventsLog eventsLog, IEventBus eventBus)
{
this.eventBus = eventBus;
this.eventsLog = eventsLog;
}

public Task Handle(TEvent @event, CancellationToken cancellationToken)
public async Task Publish(IEventEnvelope eventEnvelope, CancellationToken ct)
{
eventsLog.PublishedEvents.Add(@event);

return Task.CompletedTask;
eventsLog.PublishedEvents.Add(eventEnvelope);
await eventBus.Publish(eventEnvelope, ct);
}
}

8 changes: 5 additions & 3 deletions Core.Testing/TestWebApplicationFactory.cs
Expand Up @@ -22,11 +22,13 @@ protected override IHost CreateHost(IHostBuilder builder)
builder.ConfigureServices(services =>
{
services.AddSingleton(eventsLog)
.AddSingleton(typeof(IEventHandler<>), typeof(EventListener<>))
.AddSingleton<IExternalEventProducer>(externalEventProducer)
.AddSingleton<IEventBus>(sp =>
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
sp.GetRequiredService<IExternalEventProducer>()))
new EventListener(eventsLog,
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
sp.GetRequiredService<IExternalEventProducer>())
)
)
.AddSingleton<IExternalCommandBus>(externalCommandBus)
.AddSingleton<IExternalEventConsumer, DummyExternalEventConsumer>();
});
Expand Down
Expand Up @@ -3,13 +3,16 @@
using ECommerce.ShoppingCarts.GettingCartById;
using FluentAssertions;
using Ogooreck.API;
using Warehouse.Api.Tests;
using static Ogooreck.API.ApiSpecification;
using Xunit;

namespace Carts.Api.Tests.ShoppingCarts.AddingProduct;

public class AddProductFixture: ApiSpecification<Program>, IAsyncLifetime
{
public AddProductFixture(): base(new ShoppingCartsApplicationFactory()) { }

public Guid ShoppingCartId { get; private set; }

public readonly Guid ClientId = Guid.NewGuid();
Expand Down
Expand Up @@ -3,13 +3,16 @@
using ECommerce.ShoppingCarts.GettingCartById;
using FluentAssertions;
using Ogooreck.API;
using Warehouse.Api.Tests;
using Xunit;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.Canceling;

public class CancelShoppingCartFixture: ApiSpecification<Program>, IAsyncLifetime
{
public CancelShoppingCartFixture(): base(new ShoppingCartsApplicationFactory()) { }

public Guid ShoppingCartId { get; private set; }

public readonly Guid ClientId = Guid.NewGuid();
Expand Down
Expand Up @@ -4,12 +4,15 @@
using FluentAssertions;
using Xunit;
using Ogooreck.API;
using Warehouse.Api.Tests;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.Confirming;

public class ConfirmShoppingCartFixture: ApiSpecification<Program>, IAsyncLifetime
{
public ConfirmShoppingCartFixture(): base(new ShoppingCartsApplicationFactory()) { }

public Guid ShoppingCartId { get; private set; }

public readonly Guid ClientId = Guid.NewGuid();
Expand Down
Expand Up @@ -5,12 +5,13 @@
using ECommerce.ShoppingCarts.ProductItems;
using FluentAssertions;
using Ogooreck.API;
using Warehouse.Api.Tests;
using Xunit;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.Opening;

public class OpenShoppingCartTests: IClassFixture<TestWebApplicationFactory<Program>>
public class OpenShoppingCartTests: IClassFixture<ShoppingCartsApplicationFactory>
{
private readonly ApiSpecification<Program> API;

Expand Down Expand Up @@ -41,8 +42,8 @@ public class OpenShoppingCartTests: IClassFixture<TestWebApplicationFactory<Prog
}))
);

public OpenShoppingCartTests(TestWebApplicationFactory<Program> fixture) =>
API = ApiSpecification<Program>.Setup(fixture);
public OpenShoppingCartTests(ShoppingCartsApplicationFactory applicationFactory) =>
API = ApiSpecification<Program>.Setup(applicationFactory);

public readonly Guid ClientId = Guid.NewGuid();
}
Expand Up @@ -4,12 +4,14 @@
using FluentAssertions;
using Xunit;
using Ogooreck.API;
using Warehouse.Api.Tests;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.RemovingProduct;

public class RemoveProductFixture: ApiSpecification<Program>, IAsyncLifetime
{
public RemoveProductFixture(): base(new ShoppingCartsApplicationFactory()) { }
public Guid ShoppingCartId { get; private set; }

public readonly Guid ClientId = Guid.NewGuid();
Expand Down
@@ -0,0 +1,24 @@
using Core.Testing;
using ECommerce.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Warehouse.Api.Tests;

public class ShoppingCartsApplicationFactory: TestWebApplicationFactory<Program>
{
protected override IHost CreateHost(IHostBuilder builder)
{
var host = base.CreateHost(builder);

using var scope = host.Services.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<ECommerceDbContext>();
var database = context.Database;
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartDetailsProductItem\"");
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartShortInfo\"");
database.ExecuteSqlRaw("TRUNCATE TABLE \"ShoppingCartDetails\" CASCADE");

return host;
}
}

0 comments on commit 0cd0896

Please sign in to comment.