--- layout: default title: A friendly substitute for .NET mocking libraries ---
{% examplecode csharp %}
//Create:
var calculator = Substitute.For
CallNotReceivedException : Expected to receive call:
Add(1, 2)
Actually received (non-matching arguments indicated with '*' characters):
Add(*4*, *7*)
Add(1, *5*)
Mock, stub, fake, spy, test double? Strict or loose? Nah, just substitute for the type you need!
NSubstitute is designed for Arrange-Act-Assert (AAA) testing, so you just need to arrange how it should work, then assert it received the calls you expected once you're done. Because you've got more important code to write than whether you need a mock or a stub.
There are already some great mocking frameworks around for .NET, so why create another? We found that for all their great features, none of the existing frameworks had the succinct syntax we were craving — the code required to configure test doubles quickly obscured the intention behind our tests.
We've attempted to make the most frequently required operations obvious and easy to use, keeping less usual scenarios discoverable and accessible, and all the while maintaining as much natural language as possible.
Perfect for those new to testing, and for others who would just like to to get their tests written with less noise and less lambdas.