Usually implementing unit tests takes time and demands effort, but this task can become very simple with the help of Telerik JustMock. Check out this article on how to easily mock your code and make your tests pass successfully.
The use of mocks is indispensable for the creation of efficient unit tests. Through them, it becomes possible to simulate real scenarios, such as searching for records in a database or making a request to an external API. Telerik has a great tool to help create mocks—JustMock. See in this article how to implement unit tests mocked with JustMock in an ASP.NET Core application.
Unit testing is a subject that has gained a lot of attention lately—after all, through it we can verify that the code we write is working as expected. This guarantees that the logic behind the functionality covered by the test is actually functional and prevents many bugs from reaching the production environment. This is because the test runs the code and, if something is wrong, the test result will indicate it.
Although the use of automated tests is highly recommended in software projects, there are several situations that can make it difficult to use this practice.
Some aspects that usually make the use of automated tests difficult are:
To solve this problem, mocks are often used in order to create a simulation of the expected behavior.
Mocks are constructs that emulate the behavior of an object and make it possible to validate structures that depend on them, such as unit tests.
To assist in the implementation of mocking test techniques, there is a great option available at Telerik with JustMock. Fast, flexible and complete, JustMock is the perfect tool for creating unit tests in integrated .NET solutions.
This article will demonstrate how to implement efficient unit tests with the help of JustMock in a .NET 6 web application.
The project will be a minimal API in .NET 6 that will persist some sample data when the database is generated through the EF Core commands and will have an endpoint to return the records. Then a test project with Telerik JustMock will be implemented with a unit test using mocks to cover the main parts of the code. Find the source code here.
To create the project in Visual Studio:
The following are the project dependencies:
Create a “Models” folder and, inside it, the following class:
The context class has the database settings. Through Entity Framework Core and SQLite, all database configuration and creation will be simplified.
So, create a new “Data” folder and inside it create the class below:
In the Program.cs file add the following line of code:
In order for the database to be created, it is necessary to run the commands below:
dotnet ef migrations add InitialModel
dotnet ef database update
The service class communicates with the context class; it will be accessed by the API endpoint. Create a “Services” folder and, inside it, the following class:
Replace the code in the Program.cs file with the following:
The code above:
We will run the project just to verify that the application is working. If everything went well, the following Swagger panel will be displayed.
To create the test project, it is necessary to have the JustMock tool installed. Then, follow the steps below:
Replace the existing content in the “JustMockTest” class with the code below:
The code above:
[TestClass]
attribute[TestMethod]
attributeFakeOrders
) that generates a list of false orders to be used in the Test execution method. It is good practice to create fake objects in separate methods, as they can be reused in other methods.GetOrders_AreEqualsOne
” responsible for executing the test.In the GetOrders_AreEqualsOne
method, the context class mock is created, where the expected result is defined through the method (ReturnsCollection(FakeOrders()
), despite the dbContext.Orders
being used.
When the test is executed, no query is made in the database—after all, we are assigning the return value of the method (FakeOrders()
) to this list, that is, mocking the class and the object returned by the method.
Then the service class method is being mocked too and returning the context data, which is false, as it was mocked.
Finally, we go through the lists and compare the result with the value 1, which means that the lists are not empty—they contain the mock value of the orders, which makes the test pass without any problem.
Important: You must enable JustMock in the project, otherwise when running the test an error will be displayed. To enable it, in Visual Studio, click on the “Extensions” button located at the top. Then “JustMock” and finally “Enable Profiler.”
If you run the tests through Visual Studio Test Explorer, you will see that the test has passed, as shown in the image below:
Creating unit tests can become a simple task with the help of JustMock. In this article, we created a simple app that has an endpoint to retrieve some records from the database. Then we created a test project with JustMock and mocked all the main classes and methods present in the code, ensuring that they worked as expected.
There are many other possibilities using JustMock—feel free to explore them.