$npx -y skills add kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-autofixture-customizationAutoFixture 進階自訂化技術完整指南。當需要自訂 AutoFixture 建構器或處理特殊型別的測試資料產生規則時使用。涵蓋 DataAnnotations 自動整合、ISpecimenBuilder 實作、優先順序管理。包含 DateTime/數值範圍建構器、泛型化設計與流暢式擴充方法。 Make sure to use this skill whenever the user mentions AutoFixture customization, ISpecimenBuilder, DataAnnotations with AutoFixt
| 1 | # AutoFixture 進階:自訂化測試資料生成策略 |
| 2 | |
| 3 | ## 概述 |
| 4 | |
| 5 | 本技能涵蓋 AutoFixture 的進階自訂化功能,讓您能根據業務需求精確控制測試資料的生成邏輯。 |
| 6 | |
| 7 | ### 核心技術 |
| 8 | |
| 9 | 1. **DataAnnotations 整合**:AutoFixture 自動識別 `[StringLength]`、`[Range]` 等驗證屬性 |
| 10 | 2. **屬性範圍控制**:使用 `.With()` 配合 `Random.Shared` 動態產生隨機值 |
| 11 | 3. **自訂 ISpecimenBuilder**:實作精確控制特定屬性的建構器 |
| 12 | 4. **優先順序管理**:理解 `Insert(0)` vs `Add()` 的差異 |
| 13 | 5. **泛型化設計**:建立支援多種數值型別的可重用建構器 |
| 14 | |
| 15 | ## 安裝套件 |
| 16 | |
| 17 | ```xml |
| 18 | <PackageReference Include="AutoFixture" Version="4.18.1" /> |
| 19 | <PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" /> |
| 20 | ``` |
| 21 | |
| 22 | ## DataAnnotations 自動整合 |
| 23 | |
| 24 | AutoFixture 能自動識別 `System.ComponentModel.DataAnnotations` 的驗證屬性: |
| 25 | |
| 26 | ```csharp |
| 27 | public class Person |
| 28 | { |
| 29 | public Guid Id { get; set; } |
| 30 | [StringLength(10)] |
| 31 | public string Name { get; set; } = string.Empty; |
| 32 | [Range(10, 80)] |
| 33 | public int Age { get; set; } |
| 34 | public DateTime CreateTime { get; set; } |
| 35 | } |
| 36 | |
| 37 | [Fact] |
| 38 | public void AutoFixture_應能識別DataAnnotations() |
| 39 | { |
| 40 | var fixture = new Fixture(); |
| 41 | var person = fixture.Create<Person>(); |
| 42 | person.Name.Length.Should().Be(10); // StringLength(10) |
| 43 | person.Age.Should().BeInRange(10, 80); // Range(10, 80) |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | ## 使用 .With() 控制屬性範圍 |
| 48 | |
| 49 | ### 固定值 vs 動態值 |
| 50 | |
| 51 | ```csharp |
| 52 | // ❌ 固定值:只執行一次,所有物件相同值 |
| 53 | .With(x => x.Age, Random.Shared.Next(30, 50)) |
| 54 | |
| 55 | // ✅ 動態值:每個物件都重新計算 |
| 56 | .With(x => x.Age, () => Random.Shared.Next(30, 50)) |
| 57 | ``` |
| 58 | |
| 59 | ### Random.Shared 的優點 |
| 60 | |
| 61 | | 特性 | `new Random()` | `Random.Shared` | |
| 62 | | ---------- | -------------------------- | -------------------- | |
| 63 | | 實例化方式 | 每次建立新實例 | 全域共用單一實例 | |
| 64 | | 執行緒安全 | 不是 | 是 | |
| 65 | | 效能 | 多次建立有負擔,可能重複值 | 效能更佳,避免重複值 | |
| 66 | |
| 67 | ## 自訂 ISpecimenBuilder |
| 68 | |
| 69 | 涵蓋 RandomRangedDateTimeBuilder(精確控制特定 DateTime 屬性)、ImprovedRandomRangedNumericSequenceBuilder(改進版數值範圍建構器)、泛型化 NumericRangeBuilder<TValue>(支援 int/long/decimal/double/float 等多種型別),以及流暢介面擴充方法 AddRandomRange。每個建構器皆附完整實作與使用範例。 |
| 70 | |
| 71 | > 完整 ISpecimenBuilder 實作範例請參考 [references/specimen-builder-examples.md](references/specimen-builder-examples.md) |
| 72 | |
| 73 | ## 優先順序管理:Insert(0) vs Add() |
| 74 | |
| 75 | AutoFixture 內建的 `RangeAttributeRelay`、`NumericSequenceGenerator` 可能比自訂建構器有更高優先順序: |
| 76 | |
| 77 | ```csharp |
| 78 | // ❌ 可能失效:被內建建構器攔截 |
| 79 | fixture.Customizations.Add(new MyNumericBuilder(30, 50, "Age")); |
| 80 | |
| 81 | // ✅ 正確:確保最高優先順序 |
| 82 | fixture.Customizations.Insert(0, new MyNumericBuilder(30, 50, "Age")); |
| 83 | ``` |
| 84 | |
| 85 | ### int vs DateTime 處理差異 |
| 86 | |
| 87 | | 型別 | 內建建構器 | 優先順序影響 | |
| 88 | | ---------- | ------------------------------------------------- | -------------------------- | |
| 89 | | `int` | `RangeAttributeRelay`、`NumericSequenceGenerator` | 會被攔截,需用 `Insert(0)` | |
| 90 | | `DateTime` | 無特定建構器 | 不會被攔截,`Add()` 即可 | |
| 91 | |
| 92 | ## 最佳實踐 |
| 93 | |
| 94 | ### 應該做 |
| 95 | |
| 96 | 1. **善用 DataAnnotations** — 充分利用現有模型驗證規則,AutoFixture 自動產生符合限制的資料 |
| 97 | 2. **使用 Random.Shared** — 避免重複值問題,執行緒安全、效能更好 |
| 98 | 3. **Insert(0) 確保優先順序** — 自訂數值建構器務必用 `Insert(0)` |
| 99 | 4. **泛型化設計** — 建立可重用的泛型建構器,使用擴充方法提供流暢介面 |
| 100 | |
| 101 | ### 應該避免 |
| 102 | |
| 103 | 1. **忽略建構器優先順序** — 不要假設 `Add()` 一定生效 |
| 104 | 2. **過度複雜的邏輯** — 建構器保持單一職責 |
| 105 | 3. **使用 new Random()** — 可能產生重複值,非執行緒安全 |
| 106 | |
| 107 | ## 程式碼範本 |
| 108 | |
| 109 | 請參考 [templates](./templates) 資料夾中的範例檔案: |
| 110 | |
| 111 | - [dataannotations-integration.cs](./templates/dataannotations-integration.cs) - DataAnnotations 自動整合 |
| 112 | - [custom-specimen-builders.cs](./templates/custom-specimen-builders.cs) - 自訂 ISpecimenBuilder 實作 |
| 113 | - [numeric-range-extensions.cs](./templates/numeric-range-extensions.cs) - 泛型化數值範圍建構器與擴充方法 |
| 114 | |
| 115 | ## 與其他技能的關係 |
| 116 | |
| 117 | - **autofixture-basics**:本技能的前置知識,需先掌握基礎用法 |
| 118 | - **autodata-xunit-integration**:下一步學習目標,將自訂化與 xUnit 整合 |
| 119 | - **autofixture-nsubstitute-integration**:進階整合,結合 Mock 與自訂資料生成 |
| 120 | |
| 121 | ## 輸出格式 |
| 122 | |
| 123 | - 產生自訂 ISpecimenBuilder 實作類別 |
| 124 | - 產生 ICustomization 組合類別 |
| 125 | - 提供 fixture.Customizations.Insert(0, ...) 設定範例 |
| 126 | - 包含 DataAnnotations 整合與泛型化建構器程式碼 |
| 127 | |
| 128 | ## 參考資源 |
| 129 | |
| 130 | ### 原始文章 |
| 131 | |
| 132 | 本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章: |
| 133 | |
| 134 | - **Day 11 - AutoFixture 進階:自訂化測試資料生成策略** |
| 135 | - 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10375153 |
| 136 | - 範例程式碼:https://github.com/kevintsengtw/30Days_in_Testing_Samples/tree/main/day11 |
| 137 | |
| 138 | ### 官方文件 |
| 139 | |
| 140 | - [AutoFixture GitHub](https://github.com/AutoFixture/AutoFixture) |
| 141 | - [AutoFixture 官方文件](https://autofixture.github.io/) |
| 142 | - [ISpecimenBuilder 介面](https://autofixture.github.io/docs/fixture-customization/) |