70 lines
1.7 KiB
Markdown
70 lines
1.7 KiB
Markdown
# Testing Strategy
|
|
|
|
Unit and integration testing for CQRS handlers.
|
|
|
|
## Unit Testing Handlers
|
|
|
|
```csharp
|
|
public class PlaceOrderCommandHandlerTests
|
|
{
|
|
[Fact]
|
|
public async Task Handle_ShouldCreateOrder()
|
|
{
|
|
// Arrange
|
|
var repository = new Mock<IOrderRepository>();
|
|
var handler = new PlaceOrderCommandHandler(repository.Object);
|
|
|
|
var command = new PlaceOrderCommand
|
|
{
|
|
CustomerId = 1,
|
|
Items = new List<OrderItemDto>
|
|
{
|
|
new() { ProductId = 1, ProductName = "Widget", Quantity = 2, Price = 10.00m }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = await handler.HandleAsync(command, CancellationToken.None);
|
|
|
|
// Assert
|
|
repository.Verify(r => r.AddAsync(It.IsAny<Order>(), It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
}
|
|
```
|
|
|
|
## Integration Testing
|
|
|
|
```csharp
|
|
public class OrderApiTests : IClassFixture<WebApplicationFactory<Program>>
|
|
{
|
|
private readonly HttpClient _client;
|
|
|
|
public OrderApiTests(WebApplicationFactory<Program> factory)
|
|
{
|
|
_client = factory.CreateClient();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PlaceOrder_ShouldReturn201()
|
|
{
|
|
var command = new PlaceOrderCommand
|
|
{
|
|
CustomerId = 1,
|
|
Items = new List<OrderItemDto>
|
|
{
|
|
new() { ProductId = 1, ProductName = "Widget", Quantity = 2, Price = 10.00m }
|
|
}
|
|
};
|
|
|
|
var response = await _client.PostAsJsonAsync("/api/command/placeOrder", command);
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
|
}
|
|
}
|
|
```
|
|
|
|
## See Also
|
|
|
|
- [Modular Solution Overview](README.md)
|
|
- [Testing Best Practices](../../best-practices/testing.md)
|