In this blog, we are going to walk through the steps to integrate unit testing for ASP.NET Core 3.1 projects. This includes implementing sample business logic, creating mock objects for database calling using Moq, and verifying the method call using NUnit asserts.
Unit testing is a level of software testing that is implemented and executed by software developers to ensure implemented logic meets its requirement and behaves as expected.
The purposes of unit testing are:
Based on the selected implementation language need to find the tool or framework for unit testing. Here we have chosen NUnit and Moq to implement test methods.
NUnit is one of the most widely used open-source testing tools for .NET Framework. It supports building static asserts to ensure testing results. It has a console runner, which is used for batch execution of test cases.
The Setup method will be called once, before the execution of all test cases within a test class. This method arranges common mocking logic for all test methods.
The following image is an example.
The Test attribute to a method indicates that it should be run by the Test Runner application when there is a call to run test cases. The following are the criteria for adding the Test attribute to a method:
The following image is an example.
A mock object allows us to copy or clone the behavior of a class and interface. This is used when interacting with test cases.
With the use of mocks, we can set up the object, including setting parameters and return values on the function calls. Also, we can verify that the methods we set up are being called during the execution of the tested cases.
Refer to this Moq example on GitHub.
Follow these steps to create a .NET Core project in Visual Studio 2019:
Step 1: In Visual Studio 2019, select File -> New -> Project.
Step 2: In the Create New Project dialog, select the ASP.NET Core Web Application template and click Next.
Step 3: Enter the project name, and then click Create. The Project template dialog will be displayed.
Step 4: Select .NET Core, ASP.NET Core 3.1, and the API template (as shown in the following screenshot).
Follow these steps to add the test project:
Step 1: Right-click on the project solution and select the Add -> New Project option.
Step 2: Select NUnit test project (.NET Core) and click Next.
Step 3: Enter the project name, and then click Create. The created project will be displayed like the following.
Step 4: Open NuGet manager for Project1.Tests and search with keyword Moq. The result will be displayed like in the following screenshot. Select Moq and o install it.
Step 5: Ensure that the NuGet package is installed.
We are going to write a test case for a method that returns an addition of two numbers.
Step 1: Add the class file MathBL.cs with the method Sum() in Project.Main.Test.
Step 2: Select the Project1.Test project and add class file MathTestBL.cs.
Step 3: Add the methods Setup() and TestSum() inside the MathTestBL.cs class.
Step 4: Right-click on Project1.Test project dependencies and add the project reference Project.Main.Test
Step 5: Write the following code to call the Sum() method.
Sample code:
Step 6: Build the project and ensure the build succeeds. Then select View -> Test Explorer.
Steps 7: You will see the list of test methods. Right-click on MathTest.BL and then click Run.
The test run is successful, and the test case passed. You can alter the test result to seven or so to see if it fails.
We have followed a code-first approach and created a database context with the modal class Customer.cs.
Take a look at the following, where the database context and business logic already exist.
The following image demonstrates implemented generic data access logic (EntityRepository.cs) with an interface (IEntityRepository.cs).
I created CustomerBL.cs class with the following methods:
I created CustomerTestBL.cs and implemented TestGetActiveCustomers() test method like in the following image.
The following steps have been implemented:
Step 1: Select View -> Test Explorer. This will display the list of test case methods.
Step 2: Right-click on the TestGetActiveCustomer() method and click Run.
Step 3: We can see the successful execution of the test case result.
In this blog, we have learned how to integrate a test project with a .NET Core application. Also, we learned about Mock method calls and the verification of the returned result using NUnit asserts. I hope you found this blog useful.
Syncfusion provides 70+ high-performance, lightweight, modular, and responsive ASP.NET Core UI controls such as DataGrid, Charts, and Scheduler. You can use them to improve your application development.
Please provide your feedback in the comments section. You can also contact us through our support forum, support portal, or feedback portal. As always, we are happy to assist you!