Skip to main content

Testing Strategy

Unit Tests

public class CreateProductCommandHandlerTests
{
[Fact]
public async Task Handle_ValidProduct_ReturnsProductId()
{
// Arrange
var context = new Mock<IApplicationDbContext>();
var handler = new CreateProductCommandHandler(context.Object);
var command = new CreateProductCommand("Test Product", 99.99m);

// Act
var result = await handler.Handle(command, CancellationToken.None);

// Assert
Assert.True(result > 0);
}
}

Integration Tests

public class ProductsEndpointTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;

public ProductsEndpointTests(WebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}

[Fact]
public async Task GetProducts_ReturnsSuccessStatusCode()
{
var response = await _client.GetAsync("/api/products");
response.EnsureSuccessStatusCode();
}
}

Test Containers

dotnet add package Testcontainers

public class DatabaseFixture : IAsyncLifetime
{
private readonly MsSqlContainer _container = new MsSqlBuilder().Build();

public async Task InitializeAsync()
{
await _container.StartAsync();
}

public async Task DisposeAsync()
{
await _container.StopAsync();
}
}