Implementation of Unit Test using Xunit and Moq in .NET Core 6 Web API
We are going to discuss unit tests using xUnit and Moq here step-by-step in detail, I suggest you read my following blog for a basic understanding of unit test case
Unit Test using xUnit in .NET Core 6 with the help of VS Code
Introduction
• Unit Testing is a software design pattern that is used to test the smallest components in the software development phase.
• Unit Testing is used to validate the functionality which is to create expected output before going to the production environment and QA Team.
• It helps to detect issues at the early phase of the software development cycle
• There are many unit test tools that are already present while using .NET Framework like xUnit, NUnit, and many more.
xUnit
• xUnit is a free and open-source Unit testing framework for .NET development
• xUnit has many features which provide for writing a clean and good unit test case.
• It has many attributes like Fact, Theory, and many more to write test cases effectively and cleanly and also provides a mechanism to create our own attribute
Attributes of xUnit
[Fact] attribute is used by xUnit in .NET which identifies the method for unit test
[Fact]
public void EvenNumberTest() {
//Arrange
var num = 6;
//Act
bool result = Mathematics.IsEvenNumber(num);
//Assert
Assert.True(result);
}
[Theory] attribute is used to supply parameters to the test method
[Theory]
[InlineData(5)]
public void OddNumberTest(int num) {
//Act
bool result = Mathematics.IsOddNumber(num);
//Assert
Assert.True(result);
}
Test Pattern
Arrange-Act-Assert is a great way to write clean and more readable unit test cases
Arrange
In the arrange section we setup and declare some inputs and configuration variable
Act
In the Act section, we put main things and functionality like method calls, API calls, and something like that
Assert
Assert checks expected outputs and check whether they will match our functional requirement or not
Moq
- Basically, Moq is the library that is used for mocking purposes.
- Suppose our application is dependent on one or more services at that time we don’t need to initialize all the things related to that we just use the Moq library to mock some classes and functionality with dummy data.
Step 1)
Create a new .NET Core API Project
Step 2)
Configure your project
Step 3)
Provide additional information about your project
Step 4)
Project Structure
Step 5)
Install Following NuGet Packages
Step 6)
Create the Models folder and create a new class Product
Step 7)
Next, Create DbContextClass inside the Data folder for data manipulation
Step 8)
Later on, Create IProductService and ProductService class for abstraction and dependency injection inside the Services folder.
Create ProductService class
Step 9)
After that, Create a new ProductController
Step 10)
Add connection string inside app setting file
Step 11)
Next, register a few services inside Program Class
Step 12)
Add migrations and update the database using the following entity framework command after executing that into the package manager console under the main project
add-migration “First”
update-database
Step 13)
Finally, run your application and you will see swagger UI and API endpoints
This is all about the .NET Core Web API, Let’s create a new Xunit project inside which we use Moq to write the test cases.
Step 1)
Add a new Xunit project inside the existing solution
Step 2)
Configure your new project
Step 3)
Provide some additional information
Step 4)
Install Moq NuGet Package for mocking purpose
Step 5)
Create UnitTestController Class
- Here, you can see first we add reference of our main project into the current unit test project
- We mock IProductService and create an instance inside the constructor
- Next, we write one test case which takes a list of a product
- Later on, we take a list of products from our custom method which is present in the same class at the bottom
- Next, set up the list of products for the product service with some mock data
- Also, our product controller is dependent on product service and because of that, we pass the product service object inside the product controller constructor to resolve some dependencies.
- In the act section, we call the ProductList method of the controller.
- Finally, in the assert section, we check actual and expected results using a few conditions
Similarly, all the test cases are worked step by step
Step 6)
Next, go to the Test Section at the top of Visual Studio and open the Test Explorer inside that you can see all the test cases which we write inside the UnitTestControllerClass
Step 7)
Final Project Structure
Step 8)
Finally, run your test cases and check if they will be worked properly or not, also if you want to debug a test case simply right-click on the test case and click on debug after attaching the debugger point inside the test case
Conclusion
We see the introduction of unit tests and some attributes with patterns. After that, discussed Moq and its usage. Also, step-by-step implementation of using .NET Core 6.