nsubstitute-logo

How to Mock using NSubstitute

nsubstitute-logo

Unit tests are code written to test small pieces of functionality of big programs. Performing unit tests is always designed to be simple, A “UNIT” in this sense is the smallest component of the large code part that makes sense to test, mainly a method out of many methods of some class.

Unit tests take milliseconds, can be run at the press of a button, and don’t necessarily require any knowledge of the system at large. And when writing unit tests, you need to focus on the UNIT at hand and not consider other dependencies like services or variables. To accomplish that we need to use mocking. There are many Mocking frameworks out there, but today we will check NSubstitue.


Stay In Touch

Subscribe to our mailing list to stay updated on topics and videos related to .NET, Azure, and DevOps!

By submitting your information, you’re giving us permission to email you. You may unsubscribe at any time.


To get started, first you need to add the NSubstitute package to your existing project, run the following

dotnet add package NSubstitute

Now let’s say that we have the following basic interface.

public interface IClaculator
{
    int Add(int x, int y);

}

This is a small test using XUnit and NSubstitute

using NSubstitute;

[Fact]
public void Calculator_Add_Test()
{
    ICalculator calculator = Substitute.For<ICalculator>();

    calculator.Add(1, 2).Returns(3);

    var result = calculator.Add(1, 2);
    Assert.Equal(3, result);
}

let’s break it down. First, to create the mock object, we are using Substitute.For<T> and to tell our mock/substitute what it should return we are using the following syntax calculator.Add(1, 2).Returns(3); here we are configuring the method Add, when it got 1 and 2 as parameters, it should return 3.

We can add more complex arguments and parameter substitutions like the following

Substitute for any argument
calculator.Add(Arg.Any<int>(), Arg.Any<int>()).Returns(10);
Substitute for any argument giving a condition
calculator.Add(Arg.Is<int>(x => x < 100), 1).Returns(10);
Returns any args
calculator.Add(1, 2).ReturnsForAnyArgs(100); // this will return 100 always.

Summary

NSubstitute is a straightforward to create mocks/substitutes for your objects. There is more you can do with NSubstitute, you can learn more about it here in the documentation or check the code here at github/nsubstitute

Recent Posts

Adding Custom Formatting to Your Classes with IFormattable
DateTime has a great feature that I often replicate in my classes: the ability for users to format the ToString output however they want. Using …
Azure-Sync: Sync your Azure App Settings to local
Azure-Sync is a handy shell script tool designed to help .NET developers working with Azure App Services. Inspired by the functionality provided by the Azure …
Implement Builders easily with Source Generator in .NET
I created a YouTube video on Source Generator in which I showcased one possible implementation. However, I feel that I didn’t fully highlight its capabilities. …
Secure On-Premise .NET Application with Azure Key Vault
Suppose you have your Web App and Database server hosted locally on your On-Premises servers. You want to use Azure Key Vault with your .NET …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.