Dotnet Testing Filesystem Testing Abstractions logo

Dotnet Testing Filesystem Testing Abstractions

Community
kevintsengtw
dotnet-testing-filesystem-testing-abstractions

使用 System.IO.Abstractions 測試檔案系統操作的專門技能。當需要測試 File、Directory、Path 等操作、模擬檔案系統時使用。涵蓋 IFileSystem、MockFileSystem、檔案讀寫測試、目錄操作測試等。 Keywords: file testing, filesystem, 檔案測試, 檔案系統測試, IFileSystem, MockFileSystem, System.IO.Abstractions, File.ReadAllText, File.WriteAllText, Directory.CreateDirectory, Path.Combine, mock file system, 檔案抽象化

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-filesystem-testing-abstractions
Stars
30
Forks
4
Bundled files
4
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.

  • 4 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 Filesystem Testing Abstractions 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-filesystem-testing-abstractions
  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-filesystem-testing-abstractions .claude/skills/dotnet-testing-filesystem-testing-abstractions
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Filesystem Testing Abstractions 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 Filesystem Testing Abstractions 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 Filesystem Testing Abstractions 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.

檔案系統測試:使用 System.IO.Abstractions 模擬檔案操作

核心原則

1. 檔案系統相依性的根本問題

傳統直接使用 System.IO 靜態類別的程式碼難以測試,原因包括:

  • 速度問題:實際磁碟 IO 比記憶體操作慢 10-100 倍
  • 環境相依:測試結果受檔案系統狀態、權限、路徑影響
  • 副作用:測試會在磁碟上留下痕跡,影響其他測試
  • 並行問題:多個測試同時操作同一檔案會產生競爭條件
  • 錯誤模擬困難:難以模擬權限不足、磁碟空間不足等異常

2. System.IO.Abstractions 解決方案

將 System.IO 靜態類別包裝成介面的套件,支援依賴注入和測試替身。

必要 NuGet 套件:

xml
<!-- 正式環境 -->
<PackageReference Include="System.IO.Abstractions" Version="22.1.0" />

<!-- 測試專案 -->
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="22.1.0" />

3. 重構步驟

步驟一:將直接使用靜態類別的程式碼改為依賴 IFileSystem

csharp
// ❌ 重構前(不可測試)
public class ConfigService
{
    public string LoadConfig(string path) => File.ReadAllText(path);
}

// ✅ 重構後(可測試)
public class ConfigService
{
    private readonly IFileSystem _fileSystem;
    public ConfigService(IFileSystem fileSystem) => _fileSystem = fileSystem;
    public string LoadConfig(string path) => _fileSystem.File.ReadAllText(path);
}

步驟二:在 DI 容器中註冊真實實作

csharp
services.AddSingleton<IFileSystem, FileSystem>();

步驟三:在測試中使用 MockFileSystem

csharp
var mockFs = new MockFileSystem(new Dictionary<string, MockFileData>
{
    ["config.json"] = new MockFileData("{ \"key\": \"value\" }")
});
var service = new ConfigService(mockFs);

MockFileSystem 測試模式

涵蓋四種核心測試模式:預設檔案狀態建立、驗證寫入結果、目錄操作測試、使用 NSubstitute 模擬 IO 異常(UnauthorizedAccessException 等)。另含進階技巧:串流操作測試、檔案資訊查詢測試、備份檔案測試。

完整 MockFileSystem 測試模式與進階技巧請參考 references/mockfilesystem-patterns.md

最佳實踐

應該這樣做

  1. 使用 Path.Combine 處理路徑 — _fileSystem.Path.Combine("configs", "app.json")
  2. 防禦性檢查檔案存在性 — 在讀取前先檢查 _fileSystem.File.Exists()
  3. 自動建立必要目錄 — 寫入前確保目錄存在
  4. 妥善處理各種 IO 異常 — UnauthorizedAccessException、IOException、DirectoryNotFoundException
  5. 每個測試使用獨立的 MockFileSystem — 確保測試隔離

應該避免

  1. 硬編碼路徑分隔符號 — 使用 Path.Combine 取代 \\ 或 /
  2. 在單元測試中使用真實檔案系統 — 使用 MockFileSystem
  3. 忽略例外處理 — 不要假設檔案一定存在

效能考量

  • MockFileSystem 速度:比真實檔案操作快 10-100 倍
  • 記憶體使用:只建立測試必需的檔案,避免模擬超大檔案

實務整合範例

請參考 templates/ 目錄下的完整實作:

輸出格式

  • 產生使用 IFileSystem 介面的服務類別
  • 產生使用 MockFileSystem 的測試類別
  • 包含檔案讀寫、目錄操作、路徑處理測試範例
  • 提供 .csproj 套件參考(System.IO.Abstractions、System.IO.Abstractions.TestingHelpers)

參考資源

原始文章

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

官方文件

相關技能

  • nsubstitute-mocking - 測試替身與模擬
  • unit-test-fundamentals - 單元測試基礎

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 Filesystem Testing Abstractions AI skill do?

使用 System.IO.Abstractions 測試檔案系統操作的專門技能。當需要測試 File、Directory、Path 等操作、模擬檔案系統時使用。涵蓋 IFileSystem、MockFileSystem、檔案讀寫測試、目錄操作測試等。 Keywords: file testing, filesystem, 檔案測試, 檔案系統測試, IFileSystem, MockFileSystem, System.IO.Abstractions, File.ReadAllText, File.WriteAllText, Directory.CreateDirectory, Path.Combine, mock file system, 檔案抽象化

Why use Dotnet Testing Filesystem Testing Abstractions on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Filesystem Testing Abstractions 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 Filesystem Testing Abstractions 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-filesystem-testing-abstractions. 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 Filesystem Testing Abstractions?

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 Filesystem Testing Abstractions?

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

Is the Dotnet Testing Filesystem Testing Abstractions 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 👇