TestDataBuilders: ValidOrder()/ValidClient() with explicit Build(); After tests migrated

Domain objects in After-side tests are constructed only through the
builders, so the upcoming OrderLines evolution changes builder internals
instead of every test. Before keeps object initializers (legacy world).

yak: testdatabuilders-validordervalidclient-after-test-migration-91vl
This commit is contained in:
2026-09-16 22:32:13 +01:00
parent 53586d33a4
commit 08b52806de
6 changed files with 158 additions and 65 deletions
+27 -26
View File
@@ -1,4 +1,6 @@
using After;
using static BeforeAfter.Tests.TestData.ClientBuilder;
using static BeforeAfter.Tests.TestData.OrderBuilder;
using System.Reflection;
namespace BeforeAfter.Tests;
@@ -7,6 +9,11 @@ namespace BeforeAfter.Tests;
/// Tests for the AFTER situation (Yak 02): plain domain objects (no
/// <see cref="DbBase"/>), DTOs (records) carrying the data including the
/// relations, hand-written mappers, and a WebApp that consumes only DTOs.
///
/// Domain objects in these tests are constructed through the TestDataBuilders
/// (<see cref="OrderBuilder.ValidOrder"/> / <see cref="ClientBuilder.ValidClient"/>)
/// so the tests survive domain evolution; DTO initializers stay literal —
/// DTOs are the transport shapes under test.
/// </summary>
public class AfterTests
{
@@ -42,7 +49,7 @@ public class AfterTests
public void ToClientDto_maps_all_scalar_fields()
{
var id = Guid.NewGuid();
var client = new Client { Id = id, Name = "Acme" };
var client = ValidClient().WithId(id).WithName("Acme").Build();
var dto = Mappers.ToClientDto(client);
@@ -54,13 +61,13 @@ public class AfterTests
public void ToOrderDto_maps_all_scalar_fields()
{
var id = Guid.NewGuid();
var order = new Order { Id = id, Description = "one", TotalInCents = 15000 };
var order = ValidOrder().WithId(id).WithDescription("one").WithTotal(150_00).Build();
var dto = Mappers.ToOrderDto(order);
Assert.Equal(id, dto.Id);
Assert.Equal("one", dto.Description);
Assert.Equal(15000, dto.TotalInCents);
Assert.Equal(150_00, dto.TotalInCents);
}
[Fact]
@@ -79,13 +86,13 @@ public class AfterTests
public void FromOrderDto_maps_all_scalar_fields()
{
var id = Guid.NewGuid();
var dto = new OrderDto { Id = id, Description = "one", TotalInCents = 15000 };
var dto = new OrderDto { Id = id, Description = "one", TotalInCents = 150_00 };
var order = Mappers.FromOrderDto(dto);
Assert.Equal(id, order.Id);
Assert.Equal("one", order.Description);
Assert.Equal(15000, order.TotalInCents);
Assert.Equal(150_00, order.TotalInCents);
}
// ---------------------------------------------------------------------
@@ -95,9 +102,8 @@ public class AfterTests
[Fact]
public void ToClientDto_maps_orders_and_keeps_the_back_reference_consistent()
{
var client = new Client { Name = "Acme" };
var order = new Order { Description = "one" };
client.Orders.Add(order);
var order = ValidOrder().WithDescription("one").Build();
var client = ValidClient().WithName("Acme").WithOrder(order).Build();
var dto = Mappers.ToClientDto(client);
var orderDto = Assert.Single(dto.Orders);
@@ -130,9 +136,8 @@ public class AfterTests
[Fact]
public void Domain_round_trip_preserves_all_state()
{
var client = new Client { Id = Guid.NewGuid(), Name = "Acme" };
var order = new Order { Id = Guid.NewGuid(), Description = "one", TotalInCents = 15000 };
client.Orders.Add(order);
var order = ValidOrder().WithId(Guid.NewGuid()).WithDescription("one").WithTotal(150_00).Build();
var client = ValidClient().WithId(Guid.NewGuid()).WithName("Acme").WithOrder(order).Build();
var roundTrip = Mappers.FromClientDto(Mappers.ToClientDto(client));
@@ -149,9 +154,8 @@ public class AfterTests
[Fact]
public void Dto_round_trip_preserves_all_state()
{
var client = new Client { Id = Guid.NewGuid(), Name = "Acme" };
var order = new Order { Id = Guid.NewGuid(), Description = "one" };
client.Orders.Add(order);
var order = ValidOrder().WithId(Guid.NewGuid()).WithDescription("one").WithTotal(150_00).Build();
var client = ValidClient().WithId(Guid.NewGuid()).WithName("Acme").WithOrder(order).Build();
var dto = Mappers.ToClientDto(client);
var dtoOrder = dto.Orders[0];
@@ -172,9 +176,8 @@ public class AfterTests
[Fact]
public void ToClientDto_does_not_mutate_the_domain()
{
var client = new Client { Id = Guid.NewGuid(), Name = "Acme" };
var order = new Order { Id = Guid.NewGuid(), Description = "one" };
client.Orders.Add(order);
var order = ValidOrder().WithId(Guid.NewGuid()).WithDescription("one").Build();
var client = ValidClient().WithId(Guid.NewGuid()).WithName("Acme").WithOrder(order).Build();
var ordersCount = client.Orders.Count;
Mappers.ToClientDto(client);
@@ -247,9 +250,8 @@ public class AfterTests
public void After_WebApp_consumes_dtos()
{
var app = new WebApp();
var client = new Client { Id = Guid.NewGuid(), Name = "Acme" };
var order = new Order { Id = Guid.NewGuid(), Description = "order one", TotalInCents = 15000 };
client.Orders.Add(order);
var order = ValidOrder().WithId(Guid.NewGuid()).WithDescription("order one").WithTotal(150_00).Build();
var client = ValidClient().WithId(Guid.NewGuid()).WithName("Acme").WithOrder(order).Build();
var view = app.ShowClient(Mappers.ToClientDto(client));
@@ -266,9 +268,8 @@ public class AfterTests
public void DbContext_saves_plain_domain_objects_and_tracks_them()
{
var db = new DbContext();
var client = new Client { Name = "Acme" };
var order = new Order { Description = "one" };
client.Orders.Add(order);
var order = ValidOrder().WithDescription("one").Build();
var client = ValidClient().WithName("Acme").WithOrder(order).Build();
Assert.Equal(Guid.Empty, client.Id);
db.Save(client);
@@ -286,7 +287,7 @@ public class AfterTests
public void Saving_twice_does_not_reassign_the_id()
{
var db = new DbContext();
var client = new Client { Name = "Acme" };
var client = ValidClient().WithName("Acme").Build();
db.Save(client);
var id = client.Id;
@@ -300,7 +301,7 @@ public class AfterTests
public void A_saved_domain_object_keeps_no_reference_to_the_context()
{
var db = new DbContext();
var client = new Client { Name = "Acme" };
var client = ValidClient().WithName("Acme").Build();
db.Save(client);
// Saved (the id was assigned by the context) ...
@@ -313,4 +314,4 @@ public class AfterTests
var dto = Mappers.ToClientDto(client);
Assert.Equal(client.Id, dto.Id); // the id flows on into the DTO
}
}
}