What are Mocking Frameworks?
Mocking Frameworks (Moq, NSubstitute, Rhino Mocks, FakeItEasy, and NMock3) are used to create fake objects. We can stub, i.e., completely replace the body of member and function. It is used to isolate each dependency and help developers in performing unit testing in a concise, quick, and reliable way.
Creating mock objects manually is very difficult and time-consuming. So, to increase your productivity, you can go for the automatic generation of mock objects by using a Mocking Framework. A developer can build his/her unit test by using any of the NUnit, MbUnit, MSTest, xUnit etc. unit test frameworks.
Description
In this article, we will configure Moq Mocking Framework with xUnit.net unit testing framework.
Prerequisite
Mocking Frameworks (Moq, NSubstitute, Rhino Mocks, FakeItEasy, and NMock3) are used to create fake objects. We can stub, i.e., completely replace the body of member and function. It is used to isolate each dependency and help developers in performing unit testing in a concise, quick, and reliable way.
Creating mock objects manually is very difficult and time-consuming. So, to increase your productivity, you can go for the automatic generation of mock objects by using a Mocking Framework. A developer can build his/her unit test by using any of the NUnit, MbUnit, MSTest, xUnit etc. unit test frameworks.
Description
In this article, we will configure Moq Mocking Framework with xUnit.net unit testing framework.
Prerequisite
Step 1
Create a library project ("TDD.xUnit.net.Client") and set up xUnit.net unit test project. If you are not aware of setting up Xunit unit test project, then refer to the article - Setup xUnit.net Unit Testing In Class Library Project.
Step 2
Create a library project ("Calculator.Lib") and a test project ("TDD.xUnit.net.Client") as in the following screen.
Create a library project ("TDD.xUnit.net.Client") and set up xUnit.net unit test project. If you are not aware of setting up Xunit unit test project, then refer to the article - Setup xUnit.net Unit Testing In Class Library Project.
Step 2
Create a library project ("Calculator.Lib") and a test project ("TDD.xUnit.net.Client") as in the following screen.

Step 3
Open "ICalculator.cs" file from "Calculator.Lib" project and add the following lines of code.
Open "ICalculator.cs" file from "Calculator.Lib" project and add the following lines of code.
- namespace CalculatorLib
- {
- public interface ICalculator
- {
- decimal Add(decimal num1, decimal num2);
- decimal Substract(decimal num1, decimal num2);
- decimal Multiply(decimal num1, decimal num2);
- decimal Divide(decimal num1, decimal num2);
- }
- }
Step 4
So far, we have not implemented interface "ICalculator.cs". Now, let's try to write a unit test case. What do you think? Is it possible to write a unit test case for ICalculator interface without using a mocking framework? Think for a while! No, absolutely not, because you cannot create an instance of an interface so you can't write a unit test for the interface without a mocking framework.
Step 5
Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and add the following lines of code.
So far, we have not implemented interface "ICalculator.cs". Now, let's try to write a unit test case. What do you think? Is it possible to write a unit test case for ICalculator interface without using a mocking framework? Think for a while! No, absolutely not, because you cannot create an instance of an interface so you can't write a unit test for the interface without a mocking framework.
Step 5
Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and add the following lines of code.
- using CalculatorLib;
- using System.Diagnostics.CodeAnalysis;
- using Xunit;
- namespace TDD.xUnit.net.Client
- {
- [ExcludeFromCodeCoverage]
- public class CalculatorTests
- {
- [Fact]
- public void PassingTest()
- {
- var calculator = new ICalculator();
- Assert.Equal(4, calculator.Add(2, 2));
- }
- }
- }
Step 6
Now, let us install the Moq Mocking Framework. You can install this either by running a command in Package Manager Console or just by searching NuGet Package Manager. Let's install it by Package Manager Console by running the below command.
Now, let us install the Moq Mocking Framework. You can install this either by running a command in Package Manager Console or just by searching NuGet Package Manager. Let's install it by Package Manager Console by running the below command.
- Install-Package Moq -Version 4.9.0
From the above screen, you can see Moq mocking framework which has been installed in the highlighted project. Now, let's write the unit test for interface ICalculator.
Step 7
Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and replace the following lines of code.
Open "CalculatorTests.cs" file from "TDD.xUnit.net.Client" project and replace the following lines of code.
- using CalculatorLib;
- using Moq;
- using System.Diagnostics.CodeAnalysis;
- using Xunit;
- namespace TDD.xUnit.net.Client
- {
- [ExcludeFromCodeCoverage]
- public class CalculatorTests
- {
- [Fact]
- public void PassingTest()
- {
- //var calculator = new ICalculator();
- //Assert.Equal(4, calculator.Add(2, 2));
- var calculator = new Mock<ICalculator>();
- calculator.Setup(x => x.Add(2, 2)).Returns(4);
- Assert.Equal(4, calculator.Object.Add(2, 2));
- }
- }
- }
Now, you can see we are able to write, debug, and run the unit test case as in the below screen.

What are the other options except for mocking framework? Here, you can create a fake object manually without using the mocking framework. You can create FakeCalculator class just by inheriting ICalculator class. Let's create a fake object manually.
Step 8
Open "FakeCalculator.cs" file from "Calculator.Lib" project and add the following lines of code.
Open "FakeCalculator.cs" file from "Calculator.Lib" project and add the following lines of code.

raghubir singhPosted Apr 4, 2021, 12:43 PM
Hi Dinesh, This is really very valuable information needed. I understood all at once. Thank you so much.
asmaa adelPosted Aug 28, 2020, 4:22 PM
Hello, Dinesh , thanks for your article I helps me a lot to understand moq , I'm looking forwars your next topic