books/backend/Books.Api.Tests/Infrastructure/IntegrationTestBase.cs

39 lines
1.2 KiB
C#
Raw Normal View History

using EventFlow;
using EventFlow.Aggregates;
using Microsoft.Extensions.DependencyInjection;
namespace Books.Api.Tests.Infrastructure;
/// <summary>
/// Base class for integration tests with isolated database per test class.
/// Uses IClassFixture pattern - each test class gets its own database.
/// </summary>
public abstract class IntegrationTestBase
: IClassFixture<TestWebApplicationFactory>, IDisposable
{
protected readonly TestWebApplicationFactory Factory;
protected readonly HttpClient Client;
private readonly IServiceScope _scope;
protected IntegrationTestBase(TestWebApplicationFactory factory)
{
Factory = factory;
Client = factory.CreateGraphQLClient();
_scope = factory.Services.CreateScope();
}
protected IServiceProvider Services => _scope.ServiceProvider;
protected ICommandBus CommandBus => Services.GetRequiredService<ICommandBus>();
protected IAggregateStore AggregateStore => Services.GetRequiredService<IAggregateStore>();
protected T GetService<T>() where T : notnull
=> Services.GetRequiredService<T>();
public void Dispose()
{
_scope?.Dispose();
GC.SuppressFinalize(this);
}
}