Commit: 635a0eb
Parent: 93d67e5

Test `OrderService`

Mårten Åsberg committed on 2026-02-21 at 18:12
IMPROVEMENTS.md +6 -0
diff --git a/IMPROVEMENTS.md b/IMPROVEMENTS.md
index 18a1a88..0e27ed8 100644
@@ -5,3 +5,9 @@
Separate domain models from API. Right now we need to consider the shape of the API when updating the "domain" models.
Document all response types and status codes in the OpenAPI specification.
`CancellationToken`s are never a bad idea.
Use an injected `TimeProvider`. This will help reduce flakey unit tests as the tests no longer rely on actual wall time.
Non of the `OrderServiceTests` are very interesting, because there's no interesting logic in the `OrderService`.
src/Walley.Checkout.Api.Tests/OrderServiceTests.cs +97 -0
diff --git a/src/Walley.Checkout.Api.Tests/OrderServiceTests.cs b/src/Walley.Checkout.Api.Tests/OrderServiceTests.cs
new file mode 100644
index 0000000..79c2099
@@ -0,0 +1,97 @@
using FluentAssertions;
using Xunit;
using Walley.Checkout.Api.Models;
using Walley.Checkout.Api.Services;
namespace Walley.Checkout.Api.Tests;
public class OrderServiceTests
{
private readonly OrderService _sut;
public OrderServiceTests()
{
_sut = new OrderService();
}
[Fact]
public async Task GetAllOrdersAsync_ShouldReturnAllOrders()
{
// Act
var orders = await _sut.GetAllOrdersAsync();
// Assert
orders.Should().HaveCount(3);
}
[Fact]
public async Task GetOrderByIdAsync_ShouldReturnTheSoughtOrder()
{
// Act
var order = await _sut.GetOrderByIdAsync("ORD-001");
// Assert
order.Should().NotBeNull();
order!.Id.Should().Be("ORD-001");
}
[Fact]
public async Task GetOrderByIdAsync_ShouldReturnNull_WhenTheSoughtOrderIsNotFound()
{
// Act
var order = await _sut.GetOrderByIdAsync("ORD-004");
// Assert
order.Should().BeNull();
}
[Fact]
public async Task CreateOrderAsync_ShouldSetCalculatedValuesOnTheOrder()
{
// Arrange
var order = new Order
{
CustomerName = "A.N. Other",
CustomerEmail = "a.n.other@example.com",
Currency = "SEK",
Lines = new List<OrderLine>
{
new() { ProductName = "Product A", Quantity = 2, UnitPrice = 3m },
new() { ProductName = "Product B", Quantity = 1, UnitPrice = 6m },
}
};
// Act
order = await _sut.CreateOrderAsync(order);
// Assert
order.Id.Should().NotBeEmpty();
order.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
order.Status.Should().Be(OrderStatus.Pending);
order.TotalAmount.Should().Be(12m);
}
[Fact]
public async Task CreateOrderAsync_ShouldPersistTheOrder()
{
// Arrange
var order = new Order
{
CustomerName = "A.N. Other",
CustomerEmail = "a.n.other@example.com",
Currency = "SEK",
Lines = new List<OrderLine>
{
new() { ProductName = "Product A", Quantity = 2, UnitPrice = 3m },
new() { ProductName = "Product B", Quantity = 1, UnitPrice = 6m },
}
};
// Act
order = await _sut.CreateOrderAsync(order);
// Assert
var persistedOrder = await _sut.GetOrderByIdAsync(order.Id);
persistedOrder.Should().NotBeNull();
}
}