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.