Dotnet Testing Advanced Aspnet Integration Testing logo

Dotnet Testing Advanced Aspnet Integration Testing

Community
kevintsengtw
dotnet-testing-advanced-aspnet-integration-testing

ASP.NET Core 整合測試的專門技能。當需要測試 Web API 端點、HTTP 請求/回應、中介軟體、依賴注入時使用。涵蓋 WebApplicationFactory、TestServer、HttpClient 測試、記憶體資料庫配置等。 Keywords: integration testing, 整合測試, web api testing, WebApplicationFactory, TestServer, HttpClient testing, controller testing, endpoint testing, 端點測試, RESTful API testing, Microsoft.AspNetCore.Mvc.Testing, CreateClient, ConfigureWebHost, AwesomeAssertions.Web, Be200Ok, Be404NotFound, middleware testing, 中介軟體測試, dependency injection testing

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-advanced-aspnet-integration-testing
Stars
30
Forks
4
Bundled files
8
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 8 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by kevintsengtw on GitHub. Read the source before you install it.

Installation

Install the Dotnet Testing Advanced Aspnet Integration Testing AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-advanced-aspnet-integration-testing
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

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-advanced-aspnet-integration-testing .claude/skills/dotnet-testing-advanced-aspnet-integration-testing
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Advanced Aspnet Integration Testing in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Dotnet Testing Advanced Aspnet Integration Testing on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Dotnet Testing Advanced Aspnet Integration Testing is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

ASP.NET Core 整合測試指南

核心概念

整合測試的兩種定義

  1. 多物件協作測試 — 將兩個以上的類別做整合,測試它們之間的運作是否正確
  2. 外部資源整合測試 — 使用到資料庫、外部服務、檔案等外部資源的測試

為什麼需要整合測試?

  • 確保多個模組整合後能正確工作
  • 單元測試無法涵蓋的整合點:Routing、Middleware、Request/Response Pipeline
  • 確認 WebApplication 的整合與設定是否正確
  • 確認異常處理是否完善

測試金字塔定位

測試類型測試範圍執行速度維護成本建議比例
單元測試單一類別/方法很快低70%
整合測試多個元件中等中等20%
端對端測試完整流程慢高10%

原則一:使用 WebApplicationFactory 建立測試環境

透過 WebApplicationFactory<Program> 建立記憶體中的 TestServer,搭配 IClassFixture 在測試類別間共享。

基本流程

  1. 測試類別實作 IClassFixture<WebApplicationFactory<Program>>
  2. 透過建構函式注入 Factory
  3. 使用 factory.CreateClient() 建立 HttpClient
  4. 對 API 端點發送請求並驗證回應

自訂 Factory

繼承 WebApplicationFactory<Program> 並覆寫 ConfigureWebHost:

  • 移除原本的 DbContextOptions,改用 UseInMemoryDatabase
  • 使用 services.Replace() 替換外部服務為測試版本
  • 設定 builder.UseEnvironment("Testing") 使用測試環境

測試基底類別

建立 IntegrationTestBase 封裝共用邏輯:

  • Factory 與 HttpClient 初始化
  • SeedShipperAsync() 等資料準備方法
  • CleanupDatabaseAsync() 清理方法
  • 實作 IDisposable 確保資源釋放

完整程式碼範例(基本使用、自訂 Factory、測試基底類別)請參考 references/webapplicationfactory-examples.md

原則二:使用 AwesomeAssertions.Web 驗證 HTTP 回應

提供流暢的 HTTP 狀態碼斷言與強型別回應驗證:

  • 狀態碼斷言:Be200Ok()、Be201Created()、Be400BadRequest()、Be404NotFound() 等
  • Satisfy 驗證:response.Should().Be200Ok().And.Satisfy<T>(result => { ... })
  • 優勢:取代手動 ReadAsStringAsync() + JsonSerializer.Deserialize<T>() 的冗長程式碼

原則三:使用 System.Net.Http.Json 簡化 JSON 操作

  • PostAsJsonAsync(url, object) — 取代手動 JsonSerializer.Serialize + new StringContent
  • ReadFromJsonAsync<T>() — 取代手動 ReadAsStringAsync + JsonSerializer.Deserialize

完整斷言與 JSON 操作範例請參考 references/assertion-and-json-examples.md

三個層級的整合測試策略

Level特色測試重點Factory 設定
1無資料庫、無 ServiceAPI 輸入輸出、路由、狀態碼直接使用原始 Factory
2有 Service(NSubstitute)依賴注入配置、服務互動ConfigureTestServices
3完整架構 + 資料庫真實 CRUD、資料完整性InMemoryDatabase 替換

Level 1:簡單的 WebApi 專案

最簡單的形式,直接使用 WebApplicationFactory<Program> 測試各個 API 端點的輸入輸出。

Level 2:相依 Service 的 WebApi 專案

使用 NSubstitute 建立 Service stub,透過自訂 Factory 的 ConfigureTestServices 注入測試用服務。

Level 3:完整的 WebApi 專案

包含真實的資料庫操作,移除原本的 DbContextOptions 並改用 InMemoryDatabase,在測試中呼叫 EnsureCreated() 建立結構。

完整三個層級的程式碼範例請參考 references/three-level-testing-strategy.md

CRUD 操作測試

測試涵蓋範圍

操作測試重點
GET單一資源查詢、集合查詢、不存在資源回傳 404
POST建立成功回傳 201、驗證錯誤回傳 400
PUT更新成功、不存在資源處理
DELETE刪除成功回傳 204、不存在資源回傳 404

測試資料管理

  • 每個測試開始前呼叫 CleanupDatabaseAsync() 確保乾淨狀態
  • 使用 SeedShipperAsync() 等方法準備測試資料
  • 使用 Satisfy<T>() 驗證回應內容的正確性

完整的 CRUD 操作測試程式碼請參考 references/crud-test-examples.md

專案結構建議

text
tests/
├── Sample.WebApplication.UnitTests/           # 單元測試
├── Sample.WebApplication.Integration.Tests/   # 整合測試
│   ├── Controllers/                           # 控制器整合測試
│   ├── Infrastructure/                        # 測試基礎設施
│   │   └── CustomWebApplicationFactory.cs
│   ├── IntegrationTestBase.cs                 # 測試基底類別
│   └── GlobalUsings.cs
└── Sample.WebApplication.E2ETests/            # 端對端測試

常見錯誤排除

'ObjectAssertions' 未包含 'Be200Ok' 的定義

此錯誤通常是因為安裝了不相容的斷言套件組合。請確認基礎斷言庫與 Web 擴充套件版本一致。

Program 類別無法存取

確保主專案的 Program.cs 包含以下宣告,讓測試專案可以存取:

csharp
public partial class Program { }

測試資料互相干擾

  • 每個測試開始前清理資料庫
  • 不要依賴其他測試的執行順序
  • 使用唯一識別碼避免資料衝突

套件相容性

基礎斷言庫正確的套件
FluentAssertions < 8.0.0FluentAssertions.Web
FluentAssertions >= 8.0.0FluentAssertions.Web.v8
AwesomeAssertions >= 8.0.0AwesomeAssertions.Web
xml
<!-- 推薦組合 -->
<PackageReference Include="AwesomeAssertions" Version="9.4.0" />
<PackageReference Include="AwesomeAssertions.Web" Version="1.9.6" />

最佳實踐

應該做

  1. 獨立測試專案 — 整合測試與單元測試分離
  2. 測試資料隔離 — 每個測試案例有獨立的資料準備和清理
  3. 使用基底類別 — 共用設定和輔助方法放在基底類別
  4. 明確的命名 — 使用三段式命名法(方法_情境_預期)
  5. 適當的測試範圍 — 專注於整合點,不過度測試

應該避免

  1. 混合測試類型 — 不要將單元測試和整合測試放在同一專案
  2. 測試相依性 — 每個測試應獨立,不依賴其他測試的執行順序
  3. 過度模擬 — 整合測試應盡量使用真實元件
  4. 忽略清理 — 測試完成後要清理測試資料
  5. 硬編碼資料 — 使用工廠方法或 Builder 模式建立測試資料

輸出格式

  • 產生 CustomWebApplicationFactory.cs,配置測試用 DI 容器與資料庫替換
  • 產生 IntegrationTestBase.cs 測試基底類別,包含資料準備與清理方法
  • 產生控制器測試類別(*ControllerTests.cs),涵蓋 CRUD 操作驗證
  • 修改測試專案 .csproj,加入 Microsoft.AspNetCore.Mvc.Testing 與 AwesomeAssertions.Web
  • 確保主專案 Program.cs 包含 public partial class Program { } 以支援測試存取

參考資源

範例檔案

原始文章

官方文件

相關技能

  • unit-test-fundamentals - 單元測試基礎
  • nsubstitute-mocking - 使用 NSubstitute 進行模擬
  • awesome-assertions-guide - AwesomeAssertions 流暢斷言
  • testcontainers-database - 使用 Testcontainers 進行容器化資料庫測試

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Dotnet Testing Advanced Aspnet Integration Testing AI skill do?

ASP.NET Core 整合測試的專門技能。當需要測試 Web API 端點、HTTP 請求/回應、中介軟體、依賴注入時使用。涵蓋 WebApplicationFactory、TestServer、HttpClient 測試、記憶體資料庫配置等。 Keywords: integration testing, 整合測試, web api testing, WebApplicationFactory, TestServer, HttpClient testing, controller testing, endpoint testing, 端點測試, RESTful API testing, Microsoft.AspNetCore.Mvc.Testing, CreateClient, ConfigureWebHost, AwesomeAssertions.Web, Be200Ok, Be404NotFound, middleware testing, 中介軟體測試, dependency injection testing

Why use Dotnet Testing Advanced Aspnet Integration Testing on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Advanced Aspnet Integration Testing is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Dotnet Testing Advanced Aspnet Integration Testing in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-advanced-aspnet-integration-testing. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Dotnet Testing Advanced Aspnet Integration Testing?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Dotnet Testing Advanced Aspnet Integration Testing?

As many as you like. As long as a model supports skills, you can use Dotnet Testing Advanced Aspnet Integration Testing with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Dotnet Testing Advanced Aspnet Integration Testing AI skill free?

Yes. It is published on GitHub by kevintsengtw under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇