Skip to content

Instantly share code, notes, and snippets.

View cuteribs's full-sized avatar
🏠
Working from home

cuteribs cuteribs

🏠
Working from home
View GitHub Profile

Moq vs. NSubstitute: A Detailed Comparison

In the world of .NET unit testing, Moq and NSubstitute are two of the most popular mocking frameworks. They both aim to help developers create test doubles to test units of code in isolation. Although their goal is the same, they have significant differences in design philosophy, API syntax, and user experience.

1. Core Design Philosophy

This is the most fundamental difference between the two and influences everything else.

  • Moq: Record-Replay/Expectation Model
  • Philosophy: Moq's API is built around the core concepts of "setting up an expectation (Setup)" and "verifying it (Verify)". You first explicitly tell the mock object, "When you receive this specific call, this is how you should react." Then, at the end of the test, you can verify if that call actually happened.

Moq 与 NSubstitute 详细对比

在 .NET 的单元测试世界中,Moq 和 NSubstitute 是两个最受欢迎的模拟(Mocking)框架。它们都旨在帮助开发者创建测试替身(Test Doubles),以便在隔离的环境中测试代码单元。尽管目标相同,但它们在设计理念、API 语法和使用体验上存在显著差异。

1. 核心设计理念

这是两者最根本的区别,影响了它们的一切。

  • Moq: "录制-回放/期望" 模式 (Record-Replay/Expectation)
    • 理念: Moq 的 API 围绕着“设置期望 (Setup)”和“验证 (Verify)”这两个核心概念构建。你首先要明确地告诉 mock 对象:“当你接收到某个特定调用时,你应该如何反应”。然后,在测试的最后,你可以验证这个调用是否真的发生了。
@cuteribs
cuteribs / 天翼云电脑.md
Last active July 13, 2024 12:54
天翼云电脑.md
类别 厂家 型号 屏幕尺寸(寸) 操作系统 CPU 内存 存储
笔记本 中兴 W600D 14 Android 11.0 ARM MT8185 8核 2G 32G
笔记本 中兴 W600DL-T 14 Android 11.0 ARM MT8185 8核 2G 32G
笔记本 中兴 W600D SE 14 Android 11.0 RK3568 4核 2G 32G
笔记本 中兴 W601E 14 Android 11.0 RK3568 4核 2G 32G
笔记本 朝歌 Z96A 14 Android 11.0 RK3568 4核 4G 32G
笔记本 升腾 VNE210 14 Windows 10 Intel J4125 4G 64G
笔记本 视源 ML01A 14 Android 11.0 RK3568 4核 4G 32G
笔记本 AOC N142E1 14 Windows 10 Intel赛扬J4125 8G 128G
@cuteribs
cuteribs / preparations.md
Created January 5, 2024 11:38 — forked from wholroyd/preparations.md
Getting Minikube on WSL2 Ubuntu working

Windows Preparation

  1. Ensure hypervisor functionality is enabled in BIOS.

    • I know it sounds stupid, but if you already have it enabled, disable it, restart the machine, and enable it again.
    • Otherwise you will hit microsoft/WSL#5363
  2. Launch a PowerShell prompt in Administrator mode [Win+X > Windows PowerShell (Admin)]

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
@cuteribs
cuteribs / JSON Schema Notes.md
Created August 12, 2023 14:26
JSON Schema Notes

JSON Schema

. JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.

@cuteribs
cuteribs / JSON Schema Notes.md
Created August 12, 2023 14:26
JSON Schema Notes

JSON Schema

. JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
public static byte[] ParseRedisHexData(string hexData)
{
var bytes = hexData.Split("\\x", StringSplitOptions.RemoveEmptyEntries)
.SelectMany(ParseRedisHexSegment)
# Run this command below to allow WSL2 accessing open ports on HOST.
Set-NetFirewallProfile -Profile Public -DisabledInterfaceAliases "vEthernet (WSL)"
品牌 规格 TBW 质保 近期价格
移速 YSSDHB-4TN7000 7450/6500 9000 五年保 999.00
梵想 S790 7450/6500 8000 五年保 1178.00
雷克沙 ARES 7400/6500 3000 五年保 1099.00
爱国者 P7000Z 7450/6500 8000 五年保 1278.00
七彩虹 CN700 PRO 7400/6600 1280 三年保 1098.00
惠普 FX900 PRO 7400/6700 2400 五年保 1599.00
宏碁 GM7000 7400/6700 3000 五年保 1599.00
铭瑄 MS4TBNM700 7450/6750 2800 三年保 1099.00
设备 色域 sRGB 色域 Adobe RGB 色域 P3 色域 NTSC 色调响应 白点 对比度 亮度均匀度 色彩均匀度 色准 △
XPS 9360 97 76 77 71 3.0 4.0 5.0 2.0 4.5 4.40
iPad Air 99 76 76 70 4.0 4.0 5.0 3.0 4.5 0.95
iPad 9 97 74 74 69 5.0 4.0 5.0 4.5 4.5 0.82
iPlay 20 97 74 74 69 2.5 3.0 5.0 3.5 4.0 1.14
iPhone XS Max 97 72 72 67 5.0 4.5 5.0 0 0 1.25
一加 9RT 5G 100 97 100 96 4.0 3.5 5.0 0 0 1.65