Token导航 LogoToken导航TokenDH.com
运维和基础设施需要联网github未标认证来源可访问clear审计通过

dotnet-testing-nsubstitute-mockingdotnet 测试 nsubstitute 模拟

Agent Skill

用于辅助测试设计、自动化测试、用例整理和回归验证。它适合让 Agent 编写单元测试、端到端测试、测试计划或根据失败日志定位问题。使用时需要确认项目测试框架、运行命令和夹具数据,避免为了通过测试而改坏真实逻辑;涉及浏览器或外部服务时,应区分本地模拟、测试环境和生产环境。

总安装

840

周安装

34

GitHub Stars

24

下载量

264
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:dotnet-testing-nsubstitute-mocking(dotnet 测试 nsubstitute 模拟)
来源仓库:https://github.com/kevintsengtw/dotnet-testing-agent-skills
仓库路径:skills/dotnet-testing-nsubstitute-mocking
安装命令:
npx skills add https://github.com/kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-nsubstitute-mocking
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-nsubstitute-mocking

简介

dotnet-testing-nsubstitute-mocking 使用 NSubstitute 创建轻量级测试替身。

  • 模拟接口返回值、抛出异常或验证方法调用次数与顺序。
  • 避免依赖真实数据库、HTTP 服务或文件系统造成测试脆弱性。
  • 需安装 NSubstitute NuGet 包并配合 xUnit 或 MSTest 使用。
  • 推荐结合 AutoFixture 实现全自动依赖注入,减少手工配置负担。

SKILL.md

NSubstitute 測試替身指南

為什麼需要測試替身?

真實世界的程式碼通常依賴外部資源,這些依賴會讓測試變得:

  1. 緩慢 - 需要實際操作資料庫、檔案系統、網路
  2. 不穩定 - 外部服務異常導致測試失敗
  3. 難以重複 - 時間、隨機數導致結果不一致
  4. 環境依賴 - 需要特定的外部環境設定
  5. 開發阻塞 - 必須等待外部系統準備就緒

測試替身(Test Double)讓我們能夠隔離這些依賴,專注測試業務邏輯。

前置需求

套件安裝

<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="AwesomeAssertions" Version="9.4.0" />

基本 using 指令

using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
using AwesomeAssertions;
using Microsoft.Extensions.Logging;

Test Double 五大類型

根據 Gerard Meszaros 在《xUnit Test Patterns》中的定義,測試替身分為五種類型:

類型用途NSubstitute 對應
Dummy填充物件,僅滿足方法簽章Substitute.For<T>() 不設定任何行為
Stub提供預設回傳值,設定測試情境.Returns(value)
Fake簡化實作,有真實邏輯手動實作介面(如 FakeUserRepository
Spy記錄呼叫,事後驗證.Received() 驗證
Mock預設期望互動,未滿足則測試失敗.Received(n) 嚴格驗證
各類型的完整程式碼範例請參閱 references/test-double-types.md

NSubstitute 核心功能

基本替代語法

// 建立介面替代
var substitute = Substitute.For<IUserRepository>();

// 建立類別替代(需要虛擬成員)
var classSubstitute = Substitute.For<BaseService>();

// 建立多重介面替代
var multiSubstitute = Substitute.For<IService, IDisposable>();

回傳值設定

基本回傳值

// 精確參數匹配
_repository.GetById(1).Returns(new User { Id = 1, Name = "John" });

// 任意參數匹配
_service.Process(Arg.Any<string>()).Returns("processed");

// 回傳序列值
_generator.GetNext().Returns(1, 2, 3, 4, 5);

條件回傳值

// 使用委派計算回傳值
_calculator.Add(Arg.Any<int>(), Arg.Any<int>())
           .Returns(x => (int)x[0] + (int)x[1]);

// 條件匹配
_service.Process(Arg.Is<string>(x => x.StartsWith("test")))
        .Returns("test-result");

拋出例外

// 同步方法拋出例外
_service.RiskyOperation()
        .Throws(new InvalidOperationException("Something went wrong"));

// 非同步方法拋出例外
_service.RiskyOperationAsync()
        .Throws(new InvalidOperationException("Async operation failed"));

引數匹配器

// 任意值
_service.Process(Arg.Any<string>()).Returns("result");

// 特定條件
_service.Process(Arg.Is<string>(x => x.Length > 5)).Returns("long-result");

// 引數擷取
string capturedArg = null;
_service.Process(Arg.Do<string>(x => capturedArg = x)).Returns("result");
_service.Process("test");
capturedArg.Should().Be("test");

// 引數檢查
_service.Process(Arg.Is<string>(x =>
{
    x.Should().StartWith("prefix");
    return true;
})).Returns("result");

呼叫驗證

// 驗證被呼叫(至少一次)
_service.Received().Process("test");

// 驗證呼叫次數
_service.Received(2).Process(Arg.Any<string>());

// 驗證未被呼叫
_service.DidNotReceive().Delete(Arg.Any<int>());

// 驗證任意參數呼叫
_service.ReceivedWithAnyArgs().Process(default);

// 驗證呼叫順序
Received.InOrder(() =>
{
    _service.Start();
    _service.Process();
    _service.Stop();
});

實戰模式

涵蓋五種常見的 NSubstitute 實戰模式,包含完整程式碼範例:

模式說明
模式 1:依賴注入與測試設定FileBackupService 完整範例,含建構式注入與 SUT 設定
模式 2:Mock vs Stub 差異Stub 關注狀態回傳值 vs Mock 關注互動行為驗證
模式 3:非同步方法測試Returns(Task.FromResult(...)).Throws() 模式
模式 4:ILogger 驗證驗證底層 Log 方法繞過擴展方法限制
模式 5:複雜設定管理基底測試類別管理共用 Substitute 設定
完整程式碼範例請參閱 references/practical-patterns.md

引數匹配進階技巧

複雜物件匹配

[Fact]
public void CreateOrder_建立訂單_應儲存正確的訂單資料()
{
    var repository = Substitute.For<IOrderRepository>();
    var service = new OrderService(repository);

    service.CreateOrder("Product A", 5, 100);

    // 驗證物件屬性
    repository.Received(1).Save(Arg.Is<Order>(o =>
        o.ProductName == "Product A" &&
        o.Quantity == 5 &&
        o.Price == 100));
}

引數擷取與驗證

[Fact]
public void RegisterUser_註冊使用者_應產生正確的雜湊密碼()
{
    var repository = Substitute.For<IUserRepository>();
    var service = new UserService(repository);

    User capturedUser = null;
    repository.Save(Arg.Do<User>(u => capturedUser = u));

    service.RegisterUser("john@example.com", "password123");

    capturedUser.Should().NotBeNull();
    capturedUser.Email.Should().Be("john@example.com");
    capturedUser.PasswordHash.Should().NotBe("password123"); // 應該被雜湊
    capturedUser.PasswordHash.Length.Should().BeGreaterThan(20);
}

常見陷阱與最佳實踐

推薦做法

  1. 針對介面而非實作建立 Substitute // 正確:針對介面 var repository = Substitute.For<IUserRepository>(); // 錯誤:針對具體類別(除非有虛擬成員) var repository = Substitute.For<UserRepository>();
  2. 使用有意義的測試資料 // 正確:清楚表達意圖 var user = new User {Id = 123, Name = "John Doe", Email = "john@example.com"}; // 錯誤:無意義的資料 var user = new User {Id = 1, Name = "test", Email = "a@b.c"};
  3. 避免過度驗證 // 正確:只驗證重要的行為 _emailService.Received(1).SendWelcomeEmail(Arg.Any<string>()); // 錯誤:驗證所有內部實作細節 _repository.Received(1).GetById(123); _repository.Received(1).Update(Arg.Any<User>()); _validator.Received(1).Validate(Arg.Any<User>());
  4. Mock 與 Stub 的明確區分 // 正確:Stub 用於設定情境,Mock 用於驗證行為 var stubRepository = Substitute.For<IUserRepository>(); // Stub var mockLogger = Substitute.For<ILogger>(); // Mock stubRepository.GetById(123).Returns(user); service.ProcessUser(123); mockLogger.Received(1).LogInformation(Arg.Any<string>());

避免做法

  1. 避免模擬值類型 // 錯誤:DateTime 是值類型 var badDate = Substitute.For<DateTime>(); // 正確:抽象時間提供者 var dateTimeProvider = Substitute.For<IDateTimeProvider>(); dateTimeProvider.Now.Returns(new DateTime(2024, 1, 1));
  2. 避免測試與實作強耦合 // 錯誤:測試實作細節 _repository.Received(1).Query(Arg.Any<string>()); _repository.Received(1).Filter(Arg.Any<Expression<Func<User, bool>>>()); // 正確:測試行為結果 var users = service.GetActiveUsers(); users.Should().HaveCount(2);
  3. 避免設定過於複雜 // 錯誤:過多的 Substitute(可能違反 SRP) var sub1 = Substitute.For<IService1>(); var sub2 = Substitute.For<IService2>(); var sub3 = Substitute.For<IService3>(); var sub4 = Substitute.For<IService4>(); // 正確:重新思考類別職責 // 考慮是否違反單一職責原則,需要重構

識別需要替代的相依性

應該替代的

  • 外部 API 呼叫(IHttpClient、IApiClient)
  • 資料庫操作(IRepository、IDbContext)
  • 檔案系統操作(IFileSystem)
  • 網路通訊(IEmailService、IMessageQueue)
  • 時間依賴(IDateTimeProvider、TimeProvider)
  • 隨機數產生(IRandom)
  • 昂貴的計算(IComplexCalculator)
  • 記錄服務(ILogger)

不應該替代的

  • 值物件(DateTime、string、int)
  • 簡單的資料傳輸物件(DTO)
  • 純函數工具(如 AutoMapper 的 IMapper,考慮使用真實實例)
  • 框架核心類別(除非有明確需求)

疑難排解

Q1: 如何測試沒有介面的類別?

A: 確保要模擬的成員是 virtual:

public class BaseService
{
    public virtual string GetData() => "real data";
}

var substitute = Substitute.For<BaseService>();
substitute.GetData().Returns("test data");

Q2: 如何驗證方法被呼叫的順序?

A: 使用 Received.InOrder():

Received.InOrder(() =>
{
    _service.Start();
    _service.Process();
    _service.Stop();
});

Q3: 如何處理 out 參數?

A: 使用 Returns() 配合委派:

_service.TryGetValue("key", out Arg.Any<string>())
        .Returns(x =>
        {
            x[1] = "value";
            return true;
        });

Q4: NSubstitute 與 Moq 該如何選擇?

A: NSubstitute 優勢:

  • 語法更簡潔直觀
  • 學習曲線平緩
  • 沒有隱私爭議
  • 對多數測試場景足夠

選擇 NSubstitute,除非:

  • 專案已使用 Moq
  • 需要 Moq 特有的進階功能
  • 團隊已熟悉 Moq 語法

與其他技能整合

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

  • unit-test-fundamentals: 單元測試基礎與 3A 模式
  • dependency-injection-testing: 依賴注入測試策略
  • test-naming-conventions: 測試命名規範
  • test-output-logging: ITestOutputHelper 與 ILogger 整合
  • datetime-testing-timeprovider: TimeProvider 抽象化時間依賴
  • filesystem-testing-abstractions: 檔案系統依賴抽象化

範本檔案參考

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

  • templates/mock-patterns.cs: 完整的 Mock/Stub/Spy 模式範例
  • templates/verification-examples.cs: 行為驗證與引數匹配範例
  • references/practical-patterns.md: 五種實戰模式完整程式碼
  • references/test-double-types.md: Test Double 五大類型詳細範例

輸出格式

  • 產生使用 NSubstitute 的 xUnit 測試類別檔案(*Tests.cs
  • 測試方法遵循 3A(Arrange-Act-Assert)模式
  • Substitute 設定集中於測試建構函式或 Arrange 區段
  • 驗證呼叫使用 Received() / DidNotReceive() 明確斷言
  • 搭配 AwesomeAssertions 進行狀態驗證

參考資源

原始文章

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

  • Day 07 - 依賴替代入門:使用 NSubstitute

- 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10374593 - 範例程式碼:https://github.com/kevintsengtw/30Days_in_Testing_Samples/tree/main/day07

NSubstitute 官方

Test Double 理論

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Gemini CLI

29.95%
按下载量换算79

Claude Code

25.59%
按下载量换算68

Antigravity

17.03%
按下载量换算45

OpenCode

13.54%
按下载量换算36

windsurf

7.54%
按下载量换算20

github-copilot

3.47%
按下载量换算9

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills