{"id":"f855981b-7a33-4a15-9a6f-c516554204d6","shortId":"9aaNPK","kind":"skill","title":"dotnet-testing-autofixture-bogus-integration","tagline":"AutoFixture 與 Bogus 整合完整指南。當需要結合 AutoFixture 與 Bogus 產生兼具匿名性與真實感的測試資料時使用。涵蓋 SpecimenBuilder 整合、混合產生器、測試資料工廠與循環參考處理。\nMake sure to use this skill whenever the user mentions AutoFixture with Bogus, Faker integration, realistic test data, semantic test data, or HybridTestDataGenerato","description":"# AutoFixture 與 Bogus 整合應用指南\n\n## 核心概念\n\n### 為什麼需要整合？\n\n| 面向           | AutoFixture              | Bogus                    | 整合方案         |\n| -------------- | ------------------------ | ------------------------ | ---------------- |\n| 資料真實感     | 低（GUID 格式字串）      | 高（真實 Email/Phone）   | 高               |\n| 物件關聯處理   | 自動                     | 手動                     | 自動             |\n| 循環參考處理   | 內建                     | 無                       | 整合             |\n| 設定複雜度     | 低                       | 中                       | 中               |\n| 適用場景       | 單元測試                 | 整合測試/原型            | 兩者皆可         |\n\n**整合效果：** `user.Email` 從 `\"Email1a2b3c4d\"` 變為 `\"john.doe@example.com\"`，其他屬性仍由 AutoFixture 自動填充。\n\n## 套件安裝\n\n```xml\n<PackageReference Include=\"AutoFixture\" Version=\"4.18.1\" />\n<PackageReference Include=\"AutoFixture.Xunit2\" Version=\"4.18.1\" />\n<PackageReference Include=\"Bogus\" Version=\"35.6.5\" />\n<PackageReference Include=\"xunit\" Version=\"2.9.3\" />\n<PackageReference Include=\"AwesomeAssertions\" Version=\"9.4.0\" />\n```\n\n## 整合方式總覽\n\n| 整合方式                     | 適用場景           | 複雜度 |\n| ---------------------------- | ------------------ | ------ |\n| 屬性層級 SpecimenBuilder     | 特定屬性使用 Bogus | 低     |\n| 類型層級 SpecimenBuilder     | 整個類型使用 Bogus | 中     |\n| 混合產生器 (HybridGenerator) | 統一 API 整合      | 中     |\n| 整合工廠 (IntegratedFactory) | 完整測試場景建構   | 高     |\n| 自訂 AutoData 屬性           | xUnit 整合         | 低     |\n\n## 核心整合技術\n\n### ISpecimenBuilder 整合\n\n透過 `ISpecimenBuilder` 介面實現屬性層級與類型層級的整合：\n\n- **屬性層級**：根據屬性名稱判斷（如 `Email`、`Phone`、`FirstName`），使用 Bogus 產生對應的真實感資料\n- **類型層級**：為整個類型（如 `User`、`Address`）註冊 Bogus Faker 產生器\n\n### 常用 SpecimenBuilder\n\n| SpecimenBuilder          | 產生資料類型       | Bogus API               |\n| ------------------------ | ------------------ | ------------------------ |\n| `EmailSpecimenBuilder`   | Email 地址         | `f.Internet.Email()`     |\n| `PhoneSpecimenBuilder`   | 電話號碼           | `f.Phone.PhoneNumber()`  |\n| `NameSpecimenBuilder`    | 人名               | `f.Name.FirstName()`     |\n| `AddressSpecimenBuilder` | 地址               | `f.Address.FullAddress()`|\n\n### 擴充方法\n\n- `fixture.WithBogus()` — 註冊所有 Bogus SpecimenBuilder\n- `fixture.WithOmitOnRecursion()` — 處理循環參考\n- `fixture.WithSeed(seed)` — 設定隨機種子\n- `fixture.WithRepeatCount(count)` — 設定 CreateMany 預設數量\n\n> 完整內容請參閱 [references/core-integration-techniques.md](references/core-integration-techniques.md)\n\n## 循環參考處理\n\n當物件存在循環參考（如 User → Company → Employees(User)）時，使用 `OmitOnRecursionBehavior` 解決：\n\n```csharp\nvar fixture = new Fixture();\nfixture.Behaviors.OfType<ThrowingRecursionBehavior>()\n    .ToList()\n    .ForEach(b => fixture.Behaviors.Remove(b));\nfixture.Behaviors.Add(new OmitOnRecursionBehavior());\n```\n\n**效果：** 避免 StackOverflowException，循環參考的屬性設為 null 或空集合。\n\n## 自訂 AutoData 屬性\n\n```csharp\npublic class BogusAutoDataAttribute : AutoDataAttribute\n{\n    public BogusAutoDataAttribute()\n        : base(() => new Fixture().WithBogus())\n    {\n    }\n}\n\n// 使用方式\n[Theory]\n[BogusAutoData]\npublic void 使用整合資料測試(User user, Address address)\n{\n    user.Email.Should().Contain(\"@\");\n    address.City.Should().NotBeNullOrEmpty();\n}\n```\n\n## 混合產生器與測試資料工廠\n\n### HybridTestDataGenerator\n\n統一的測試資料產生 API，實作 `ITestDataGenerator` 介面：\n\n- `Generate<T>()` — 產生單一物件\n- `Generate<T>(int count)` — 產生指定數量的集合\n- `Generate<T>(Action<T> configure)` — 產生物件後進行自訂設定\n\n### IntegratedTestDataFactory\n\n完整場景建構工廠，支援進階功能：\n\n- `CreateFresh<T>()` — 每次產生全新物件\n- `CreateMany<T>(count)` — 批次建立\n- `GetCached<T>()` — 快取機制，相同類型只產生一次\n- `CreateTestScenario()` — 建立包含 Company、Users、Orders 的完整測試場景，自動建立關聯\n\n### TestBase 基底類別\n\n統一 Fixture、Generator、Factory 的初始化與 Seed 管理，提供 `Create<T>()`、`CreateMany<T>()`、`Create<T>(configure)` 等便捷方法。\n\n> 完整程式碼範例請參考 [references/hybrid-generator-and-factory.md](references/hybrid-generator-and-factory.md)\n\n## Seed 管理與可重現性\n\n### 重要限制\n\n由於 AutoFixture 和 Bogus 有不同的隨機數管理機制：\n\n- Seed 確保測試行為穩定性\n- Seed 確保資料格式一致性\n- 無法保證所有屬性值完全相同\n\n### 建議做法\n\n- **一般場景**：使用 `IntegratedTestDataFactory(seed: 12345)` 確保穩定性\n- **完全可重現**：使用單一工具，如 `Faker<User>().UseSeed(12345)`\n- **不需重現**：不設定 Seed，每次產生不同的隨機資料\n\n## 常見問題\n\n### Q1: 什麼時候該用整合方案，什麼時候用單一工具？\n\n- **用整合方案**：需要真實感資料（Email、Phone）且物件結構複雜的測試場景\n- **用純 AutoFixture**：只需要匿名資料填充的單元測試\n- **用純 Bogus**：需要完全可重現且資料格式嚴格的整合測試\n\n### Q2: SpecimenBuilder 匹配優先順序？\n\nAutoFixture 按照 `Customizations` 集合的順序匹配，先加入的 Builder 優先。使用 `fixture.Customizations.Insert(0, builder)` 可確保最高優先。\n\n### Q3: 如何為新的實體類型加入 Bogus 支援？\n\n1. 建立對應的 `ISpecimenBuilder` 實作\n2. 在 `Create()` 方法中判斷屬性名稱或類型\n3. 使用 Bogus Faker 產生對應的真實感資料\n4. 在 `WithBogus()` 擴充方法中註冊\n\n## 最佳實踐\n\n### 建議做法\n\n1. **永遠先處理循環參考** — `fixture.WithOmitOnRecursion().WithBogus()`\n2. **為常用實體建立專用 SpecimenBuilder**\n3. **使用 Seed 確保測試穩定性**\n4. **建立測試基底類別統一資料產生邏輯**\n5. **適當使用快取提升效能**\n\n### 避免事項\n\n1. 過度設計，保持簡單實用\n2. 期望整合環境完全可重現\n3. 忽略循環參考處理\n4. 在每個測試中重新建立 Fixture\n\n## 輸出格式\n\n- 產生 `ISpecimenBuilder` 實作類別檔案（如 `EmailSpecimenBuilder.cs`）\n- 產生 Fixture 擴充方法類別檔案（`FixtureBogusExtensions.cs`）\n- 產生整合測試資料工廠或混合產生器類別\n- 產生 `BogusAutoDataAttribute` 供 xUnit Theory 測試使用\n- 搭配 `OmitOnRecursionBehavior` 處理循環參考\n\n## 參考資源\n\n### 原始文章\n\n- **Day 15 - AutoFixture 與 Bogus 整合：結合兩者優勢**\n  - 鐵人賽文章：https://ithelp.ithome.com.tw/articles/10375620\n  - 範例程式碼：https://github.com/kevintsengtw/30Days_in_Testing_Samples/tree/main/day15\n\n### 官方文件\n\n- [AutoFixture GitHub](https://github.com/AutoFixture/AutoFixture)\n- [Bogus GitHub Repository](https://github.com/bchavez/Bogus)\n\n### 相關技能\n\n- [autofixture-basics](../autofixture-basics/) - AutoFixture 基礎使用\n- [autofixture-customization](../autofixture-customization/) - AutoFixture 自訂化策略\n- [autodata-xunit-integration](../autodata-xunit-integration/) - AutoData 屬性整合\n- [bogus-fake-data](../bogus-fake-data/) - Bogus 假資料產生器","tags":["dotnet","testing","autofixture","bogus","integration","agent","skills","kevintsengtw","agent-skills","ai-assisted-development","copilot-skills","csharp"],"capabilities":["skill","source-kevintsengtw","skill-dotnet-testing-autofixture-bogus-integration","topic-agent-skills","topic-ai-assisted-development","topic-copilot-skills","topic-csharp","topic-dotnet","topic-dotnet-testing","topic-github-copilot","topic-integration-testing","topic-testing","topic-unit-testing","topic-xunit"],"categories":["dotnet-testing-agent-skills"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/kevintsengtw/dotnet-testing-agent-skills/dotnet-testing-autofixture-bogus-integration","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"cli":"npx skills add kevintsengtw/dotnet-testing-agent-skills","source_repo":"https://github.com/kevintsengtw/dotnet-testing-agent-skills","install_from":"skills.sh"}},"qualityScore":"0.461","qualityRationale":"deterministic score 0.46 from registry signals: · indexed on github topic:agent-skills · 23 github stars · SKILL.md body (5,627 chars)","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill-github:v1","enrichmentVersion":1,"enrichedAt":"2026-04-24T13:02:26.198Z","embedding":null,"createdAt":"2026-04-18T23:04:24.057Z","updatedAt":"2026-04-24T13:02:26.198Z","lastSeenAt":"2026-04-24T13:02:26.198Z","tsv":"'/articles/10375620':434 '/autodata-xunit-integration':468 '/autofixture-basics':455 '/autofixture-customization':461 '/autofixture/autofixture)':444 '/bchavez/bogus)':450 '/bogus-fake-data':475 '/kevintsengtw/30days_in_testing_samples/tree/main/day15':438 '0':350 '1':357,376,392 '12345':311,318 '15':425 '2':361,380,395 '3':365,383,397 '4':370,387,399 '5':389 'action':254 'address':139,234,235 'address.city.should':238 'addressspecimenbuild':160 'api':107,149,243 'autodata':115,213,465,469 'autodata-xunit-integr':464 'autodataattribut':219 'autofixtur':4,7,12,31,44,51,86,297,333,341,426,440,453,456,459,462 'autofixture-bas':452 'autofixture-custom':458 'b':200,202 'base':222 'basic':454 'bogus':5,9,14,33,46,52,97,102,133,141,148,166,299,336,355,367,428,445,472,476 'bogus-fake-data':471 'bogusautodata':228 'bogusautodataattribut':218,221,414 'builder':346,351 'class':217 'compani':185,270 'configur':255,288 'contain':237 'count':174,251,263 'creat':285,287,363 'createfresh':260 'createmani':176,262,286 'createtestscenario':268 'csharp':192,215 'custom':343,460 'data':38,41,474 'day':424 'dotnet':2 'dotnet-testing-autofixture-bogus-integr':1 'email':129,151,329 'email/phone':60 'email1a2b3c4d':82 'emailspecimenbuild':150 'emailspecimenbuilder.cs':407 'employe':186 'f.address.fulladdress':162 'f.internet.email':153 'f.name.firstname':159 'f.phone.phonenumber':156 'factori':280 'fake':473 'faker':34,142,316,368 'firstnam':131 'fixtur':194,196,224,278,401,409 'fixture.behaviors.add':203 'fixture.behaviors.oftype':197 'fixture.behaviors.remove':201 'fixture.customizations.insert':349 'fixture.withbogus':164 'fixture.withomitonrecursion':168,378 'fixture.withrepeatcount':173 'fixture.withseed':170 'fixturebogusextensions.cs':411 'foreach':199 'generat':247,249,253,279 'getcach':265 'github':441,446 'github.com':437,443,449 'github.com/autofixture/autofixture)':442 'github.com/bchavez/bogus)':448 'github.com/kevintsengtw/30days_in_testing_samples/tree/main/day15':436 'guid':56 'hybridgener':105 'hybridtestdatagener':241 'hybridtestdatagenerato':43 'int':250 'integr':6,35,467 'integratedfactori':111 'integratedtestdatafactori':257,309 'ispecimenbuild':121,124,359,404 'itestdatagener':245 'ithelp.ithome.com.tw':433 'ithelp.ithome.com.tw/articles/10375620':432 'john.doe@example.com':84 'make':21 'mention':30 'namespecimenbuild':157 'new':195,204,223 'notbenullorempti':239 'null':210 'omitonrecursionbehavior':190,205,420 'order':272 'phone':130,330 'phonespecimenbuild':154 'public':216,220,229 'q1':324 'q2':338 'q3':353 'realist':36 'references/core-integration-techniques.md':179,180 'references/hybrid-generator-and-factory.md':291,292 'repositori':447 'seed':171,282,293,301,303,310,321,385 'semant':39 'skill':26 'skill-dotnet-testing-autofixture-bogus-integration' 'source-kevintsengtw' 'specimenbuild':17,95,100,145,146,167,339,382 'stackoverflowexcept':208 'sure':22 'test':3,37,40 'testbas':275 'theori':227,417 'tolist':198 'topic-agent-skills' 'topic-ai-assisted-development' 'topic-copilot-skills' 'topic-csharp' 'topic-dotnet' 'topic-dotnet-testing' 'topic-github-copilot' 'topic-integration-testing' 'topic-testing' 'topic-unit-testing' 'topic-xunit' 'use':24 'user':29,138,184,187,232,233,271 'user.email':80 'user.email.should':236 'usese':317 'var':193 'void':230 'whenev':27 'withbogus':225,372,379 'xml':89 'xunit':117,416,466 '一般場景':307 '不設定':320 '不需重現':319 '且物件結構複雜的測試場景':331 '中':72,73,103,109 '人名':158 '什麼時候用單一工具':326 '什麼時候該用整合方案':325 '介面':246 '介面實現屬性層級與類型層級的整合':125 '低':55,71,98,119 '使用':132,189,308,348,366,384 '使用單一工具':314 '使用整合資料測試':231 '使用方式':226 '供':415 '保持簡單實用':394 '假資料產生器':477 '優先':347 '先加入的':345 '內建':67 '兩者皆可':78 '其他屬性仍由':85 '匹配優先順序':340 '原型':77 '原始文章':423 '參考資源':422 '只需要匿名資料填充的單元測試':334 '可確保最高優先':352 '和':298 '單元測試':75 '在':362,371 '在每個測試中重新建立':400 '地址':152,161 '基底類別':276 '基礎使用':457 '套件安裝':88 '如':128,137,183,315,406 '如何為新的實體類型加入':354 '完全可重現':313 '完整內容請參閱':178 '完整場景建構工廠':258 '完整測試場景建構':112 '完整程式碼範例請參考':290 '官方文件':439 '實作':244,360 '實作類別檔案':405 '屬性':116,214 '屬性層級':94,126 '屬性整合':470 '常用':144 '常見問題':323 '建立包含':269 '建立對應的':358 '建立測試基底類別統一資料產生邏輯':388 '建議做法':306,375 '從':81 '循環參考的屬性設為':209 '循環參考處理':66,181 '快取機制':266 '忽略循環參考處理':398 '或空集合':211 '手動':64 '批次建立':264 '按照':342 '提供':284 '搭配':419 '擴充方法':163 '擴充方法中註冊':373 '擴充方法類別檔案':410 '支援':356 '支援進階功能':259 '效果':206 '整個類型使用':101 '整合':18,69,108,118,122,429 '整合完整指南':10 '整合工廠':110 '整合應用指南':47 '整合效果':79 '整合方式':91 '整合方式總覽':90 '整合方案':53 '整合測試':76 '方法中判斷屬性名稱或類型':364 '時':188 '最佳實踐':374 '有不同的隨機數管理機制':300 '期望整合環境完全可重現':396 '核心整合技術':120 '核心概念':48 '根據屬性名稱判斷':127 '格式字串':57 '每次產生不同的隨機資料':322 '每次產生全新物件':261 '永遠先處理循環參考':377 '涵蓋':16 '混合產生器':19,104 '混合產生器與測試資料工廠':240 '測試使用':418 '測試資料工廠與循環參考處理':20 '為什麼需要整合':49 '為常用實體建立專用':381 '為整個類型':136 '無':68 '無法保證所有屬性值完全相同':305 '物件關聯處理':62 '特定屬性使用':96 '產生':403,408,413 '產生兼具匿名性與真實感的測試資料時使用':15 '產生單一物件':248 '產生器':143 '產生對應的真實感資料':134,369 '產生指定數量的集合':252 '產生整合測試資料工廠或混合產生器類別':412 '產生物件後進行自訂設定':256 '產生資料類型':147 '用整合方案':327 '用純':332,335 '由於':296 '當物件存在循環參考':182 '當需要結合':11 '的初始化與':281 '的完整測試場景':273 '相同類型只產生一次':267 '相關技能':451 '真實':59 '確保測試穩定性':386 '確保測試行為穩定性':302 '確保穩定性':312 '確保資料格式一致性':304 '等便捷方法':289 '管理':283 '管理與可重現性':294 '範例程式碼':435 '結合兩者優勢':430 '統一':106,277 '統一的測試資料產生':242 '自動':63,65 '自動填充':87 '自動建立關聯':274 '自訂':114,212 '自訂化策略':463 '與':8,13,45,427 '處理循環參考':169,421 '複雜度':93 '解決':191 '設定':175 '設定複雜度':70 '設定隨機種子':172 '註冊':140 '註冊所有':165 '變為':83 '資料真實感':54 '輸出格式':402 '透過':123 '過度設計':393 '適用場景':74,92 '適當使用快取提升效能':390 '避免':207 '避免事項':391 '重要限制':295 '鐵人賽文章':431 '集合的順序匹配':344 '電話號碼':155 '需要完全可重現且資料格式嚴格的整合測試':337 '需要真實感資料':328 '面向':50 '預設數量':177 '類型層級':99,135 '高':58,61,113","prices":[{"id":"7ddbc827-5c05-49ab-8c16-b6c0ffc1529b","listingId":"f855981b-7a33-4a15-9a6f-c516554204d6","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"kevintsengtw","category":"dotnet-testing-agent-skills","install_from":"skills.sh"},"createdAt":"2026-04-18T23:04:24.057Z"}],"sources":[{"listingId":"f855981b-7a33-4a15-9a6f-c516554204d6","source":"github","sourceId":"kevintsengtw/dotnet-testing-agent-skills/dotnet-testing-autofixture-bogus-integration","sourceUrl":"https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-autofixture-bogus-integration","isPrimary":false,"firstSeenAt":"2026-04-18T23:04:24.057Z","lastSeenAt":"2026-04-24T13:02:26.198Z"}],"details":{"listingId":"f855981b-7a33-4a15-9a6f-c516554204d6","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"kevintsengtw","slug":"dotnet-testing-autofixture-bogus-integration","github":{"repo":"kevintsengtw/dotnet-testing-agent-skills","stars":23,"topics":["agent-skills","ai-assisted-development","copilot-skills","csharp","dotnet","dotnet-testing","github-copilot","integration-testing","testing","unit-testing","xunit"],"license":"mit","html_url":"https://github.com/kevintsengtw/dotnet-testing-agent-skills","pushed_at":"2026-03-31T07:28:56Z","description":"AI Agent Skills for .NET Testing - Based on 30-Day Testing Challenge (iThome Ironman 2025 Winner)","skill_md_sha":"2ef651703c4766f8b028ac548ad2bcee25923f7e","skill_md_path":"skills/dotnet-testing-autofixture-bogus-integration/SKILL.md","default_branch":"main","skill_tree_url":"https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-autofixture-bogus-integration"},"layout":"multi","source":"github","category":"dotnet-testing-agent-skills","frontmatter":{"name":"dotnet-testing-autofixture-bogus-integration","description":"AutoFixture 與 Bogus 整合完整指南。當需要結合 AutoFixture 與 Bogus 產生兼具匿名性與真實感的測試資料時使用。涵蓋 SpecimenBuilder 整合、混合產生器、測試資料工廠與循環參考處理。\nMake sure to use this skill whenever the user mentions AutoFixture with Bogus, Faker integration, realistic test data, semantic test data, or HybridTestDataGenerator, even if they don't explicitly ask for integration guidance.\nKeywords: autofixture bogus integration, autofixture bogus, bogus integration, Faker, EmailSpecimenBuilder, PhoneSpecimenBuilder, NameSpecimenBuilder, 真實感測試資料, 語意化資料, 混合產生器, HybridTestDataGenerator, OmitOnRecursionBehavior, 循環參考"},"skills_sh_url":"https://skills.sh/kevintsengtw/dotnet-testing-agent-skills/dotnet-testing-autofixture-bogus-integration"},"updatedAt":"2026-04-24T13:02:26.198Z"}}