Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions _rules/1610.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
rule_id: 1610
rule_category: testability
title: Use Test Data Builders or Object Mothers to construct test objects
severity: 2
---
When setting up test data, avoid scattering `new SomeObject(...)` constructors across many tests. This makes tests hard to maintain when the object's constructor changes, and makes it hard to understand which properties matter to the test.

Use a **Test Data Builder** when you need flexible control over many properties:

```csharp
var order = new OrderBuilder()
.WithCustomer(customer)
.WithStatus(OrderStatus.Pending)
.Build();
```

Use an **Object Mother** (a simple static factory method) when you have a small number of canonical test objects that are reused as-is:

```csharp
var order = TestOrders.PendingOrder();
var order = TestOrders.ShippedOrder();
```

Both approaches make tests more readable and resilient to object construction changes. Prefer builders when flexibility is needed, and object mothers when a few fixed examples are sufficient.