Dotnet Testing Complex Object Comparison 标志

Dotnet Testing Complex Object Comparison

社区
kevintsengtw
dotnet-testing-complex-object-comparison

處理複雜物件比對與深層驗證的專門技能。當需要比對深層物件、排除特定屬性、處理循環參照、驗證 DTO/Entity 時使用。涵蓋 BeEquivalentTo、Excluding、Including、自訂比對規則等。 Keywords: object comparison, 物件比對, deep comparison, 深層比對, BeEquivalentTo, DTO 比對, Entity 驗證, 排除屬性, 循環參照, Excluding, Including, ExcludingNestedObjects, RespectingRuntimeTypes, WithStrictOrdering, 忽略時間戳記, exclude timestamp

概览

发布者kevintsengtw
代码仓库dotnet-testing-agent-skills
技能名称dotnet-testing-complex-object-comparison
星标数
30
复刻数
4
附带文件
3
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载的 SKILL.md 文件,因此只有在请求确实匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 3 个随附文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 kevintsengtw 发布于 GitHub。请在安装前查看源内容。

安装

在 TypingMind 中安装 Dotnet Testing Complex Object Comparison AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-complex-object-comparison
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/kevintsengtw/dotnet-testing-agent-skills.git /tmp/dotnet-testing-agent-skills
mkdir -p .claude/skills
cp -r /tmp/dotnet-testing-agent-skills/skills/dotnet-testing-complex-object-comparison .claude/skills/dotnet-testing-complex-object-comparison
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Dotnet Testing Complex Object Comparison,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Dotnet Testing Complex Object Comparison。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Dotnet Testing Complex Object Comparison 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会获取指令,因此未使用的技能不会产生额外费用。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

複雜物件比對指南(Complex Object Comparison)

核心使用場景

1. 深層物件結構比對 (Object Graph Comparison)

當需要比對包含多層巢狀屬性的複雜物件時:

csharp
[Fact]
public void ComplexObject_深層結構比對_應完全相符()
{
    var expected = new Order
    {
        Id = 1,
        Customer = new Customer
        {
            Name = "John Doe",
            Address = new Address
            {
                Street = "123 Main St",
                City = "Seattle",
                ZipCode = "98101"
            }
        },
        Items = new[]
        {
            new OrderItem { ProductName = "Laptop", Quantity = 1, Price = 999.99m },
            new OrderItem { ProductName = "Mouse", Quantity = 2, Price = 29.99m }
        }
    };

    var actual = orderService.GetOrder(1);

    // 深層物件比對
    actual.Should().BeEquivalentTo(expected);
}

2. 循環參照處理 (Circular Reference Handling)

處理物件之間存在循環參照的情況:

csharp
[Fact]
public void TreeStructure_循環參照_應正確處理()
{
    // 建立具有父子雙向參照的樹狀結構
    var parent = new TreeNode { Value = "Root" };
    var child1 = new TreeNode { Value = "Child1", Parent = parent };
    var child2 = new TreeNode { Value = "Child2", Parent = parent };
    parent.Children = new[] { child1, child2 };

    var actualTree = treeService.GetTree("Root");

    // 處理循環參照
    actualTree.Should().BeEquivalentTo(parent, options =>
        options.IgnoringCyclicReferences()
               .WithMaxRecursionDepth(10)
    );
}

3-6. 進階比對模式

AwesomeAssertions 還提供多種進階比對模式:動態欄位排除(排除時間戳記、自動生成欄位)、巢狀物件欄位排除、大量資料效能最佳化比對(選擇性屬性比對、抽樣驗證策略)、以及嚴格/寬鬆排序控制。

完整程式碼範例請參閱 references/detailed-comparison-patterns.md

比對選項速查表

選項方法用途適用場景
Excluding(x => x.Property)排除特定屬性排除時間戳記、自動生成欄位
Including(x => x.Property)只包含特定屬性關鍵屬性驗證
IgnoringCyclicReferences()忽略循環參照樹狀結構、雙向關聯
WithMaxRecursionDepth(n)限制遞迴深度深層巢狀結構
WithStrictOrdering()嚴格順序比對陣列/集合順序重要時
WithoutStrictOrdering()寬鬆順序比對陣列/集合順序不重要時
WithTracing()啟用追蹤除錯複雜比對失敗

常見比對模式與解決方案

模式 1:Entity Framework 實體比對

csharp
[Fact]
public void EFEntity_資料庫實體_應排除導航屬性()
{
    var expected = new Product { Id = 1, Name = "Laptop", Price = 999 };
    var actual = dbContext.Products.Find(1);

    actual.Should().BeEquivalentTo(expected, options =>
        options.ExcludingMissingMembers()  // 排除 EF 追蹤屬性
               .Excluding(p => p.CreatedAt)
               .Excluding(p => p.UpdatedAt)
    );
}

模式 2:API Response 比對

csharp
[Fact]
public void ApiResponse_JSON反序列化_應忽略額外欄位()
{
    var expected = new UserDto 
    { 
        Id = 1, 
        Username = "john_doe" 
    };

    var response = await httpClient.GetAsync("/api/users/1");
    var actual = await response.Content.ReadFromJsonAsync<UserDto>();

    actual.Should().BeEquivalentTo(expected, options =>
        options.ExcludingMissingMembers()  // 忽略 API 額外欄位
    );
}

模式 3:測試資料建構器比對

csharp
[Fact]
public void Builder_測試資料_應匹配預期結構()
{
    var expected = new OrderBuilder()
        .WithId(1)
        .WithCustomer("John Doe")
        .WithItems(3)
        .Build();

    var actual = orderService.CreateOrder(orderRequest);

    actual.Should().BeEquivalentTo(expected, options =>
        options.Excluding(o => o.OrderNumber)  // 系統生成
               .Excluding(o => o.CreatedAt)
    );
}

錯誤訊息最佳化

提供有意義的錯誤訊息

csharp
[Fact]
public void Comparison_錯誤訊息_應清楚說明差異()
{
    var expected = new User { Name = "John", Age = 30 };
    var actual = userService.GetUser(1);

    // 使用 because 參數提供上下文
    actual.Should().BeEquivalentTo(expected, options =>
        options.Excluding(u => u.Id)
               .Because("ID 是系統自動生成的,不應納入比對")
    );
}

使用 AssertionScope 進行批次驗證

csharp
[Fact]
public void MultipleComparisons_批次驗證_應一次顯示所有失敗()
{
    var users = userService.GetAllUsers();

    using (new AssertionScope())
    {
        foreach (var user in users)
        {
            user.Id.Should().BeGreaterThan(0);
            user.Name.Should().NotBeNullOrEmpty();
            user.Email.Should().MatchRegex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        }
    }
    // 所有失敗會一起報告,而非遇到第一個失敗就停止
}

與其他技能整合

此技能可與以下技能組合使用:

  • awesome-assertions-guide: 基礎斷言語法與常用 API
  • autofixture-basics: 自動生成測試資料
  • test-data-builder-pattern: 建構複雜測試物件
  • unit-test-fundamentals: 單元測試基礎與 3A 模式

最佳實踐建議

推薦做法

  1. 優先使用屬性排除而非包含:除非只需驗證少數屬性,否則使用 Excluding 更清楚
  2. 建立可重用的排除擴充方法:避免在每個測試重複排除邏輯
  3. 為大量資料比對設定合理策略:平衡效能與驗證完整性
  4. 使用 AssertionScope 進行批次驗證:一次看到所有失敗原因
  5. 提供有意義的 because 說明:幫助未來維護者理解測試意圖

避免做法

  1. 避免過度依賴完整物件比對:考慮只驗證關鍵屬性
  2. 避免忽略循環參照問題:使用 IgnoringCyclicReferences() 明確處理
  3. 避免在每個測試重複排除邏輯:提取為擴充方法
  4. 避免對大量資料做完整深度比對:使用抽樣或關鍵屬性驗證

疑難排解

Q1: BeEquivalentTo 效能很慢怎麼辦?

A: 使用以下策略優化:

  • 使用 Including 只比對關鍵屬性
  • 對大量資料採用抽樣驗證
  • 使用 WithMaxRecursionDepth 限制遞迴深度
  • 考慮使用 AssertKeyPropertiesOnly 快速比對關鍵欄位

Q2: 如何處理 StackOverflowException?

A: 通常由循環參照引起:

csharp
options.IgnoringCyclicReferences()
       .WithMaxRecursionDepth(10)

Q3: 如何排除所有時間相關欄位?

A: 使用路徑模式匹配:

csharp
options.Excluding(ctx => ctx.Path.EndsWith("At"))
       .Excluding(ctx => ctx.Path.EndsWith("Time"))
       .Excluding(ctx => ctx.Path.Contains("Timestamp"))

Q4: 比對失敗但看不出差異?

A: 啟用詳細追蹤:

csharp
options.WithTracing()  // 產生詳細的比對追蹤資訊

範本檔案參考

本技能提供以下範本檔案:

  • templates/comparison-patterns.cs: 常見比對模式範例
  • templates/exclusion-strategies.cs: 欄位排除策略與擴充方法

輸出格式

  • 產生使用 BeEquivalentTo 的深層物件比對斷言
  • 包含 Excluding/Including 屬性過濾設定
  • 務必提及循環參照處理:即使使用者未明確問到,也應說明 IgnoringCyclicReferences() 和 WithMaxRecursionDepth(n) 的用法,因為深層巢狀物件經常會遇到循環參照問題
  • 包含 DTO/Entity 比對的完整測試程式碼

參考資源

原始文章

本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:

官方文件

相關技能

  • awesome-assertions-guide - AwesomeAssertions 基礎與進階用法
  • unit-test-fundamentals - 單元測試基礎

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Dotnet Testing Complex Object Comparison AI 技能有什么作用?

處理複雜物件比對與深層驗證的專門技能。當需要比對深層物件、排除特定屬性、處理循環參照、驗證 DTO/Entity 時使用。涵蓋 BeEquivalentTo、Excluding、Including、自訂比對規則等。 Keywords: object comparison, 物件比對, deep comparison, 深層比對, BeEquivalentTo, DTO 比對, Entity 驗證, 排除屬性, 循環參照, Excluding, Including, ExcludingNestedObjects, RespectingRuntimeTypes, WithStrictOrdering, 忽略時間戳記, exclude timestamp

为什么在 TypingMind 中使用 Dotnet Testing Complex Object Comparison?

因为您只需安装一次,就能搭配任何模型使用。Dotnet Testing Complex Object Comparison 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型,甚至可以在聊天中途切换模型,而不会影响技能正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Dotnet Testing Complex Object Comparison?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-complex-object-comparison。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Dotnet Testing Complex Object Comparison?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Dotnet Testing Complex Object Comparison 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以搭配 Dotnet Testing Complex Object Comparison 使用,包括 GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等,都可在 TypingMind 中使用您自己的 API 密钥接入。

Dotnet Testing Complex Object Comparison AI 技能免费吗?

是的。该技能源自 kevintsengtw 在 GitHub 上以 MIT 许可证发布的项目。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇