Geek Culture
Published in

Geek Culture

Unit Testing Using XUnit And MOQ In ASP.NET Core

Unit Testing. Xunit , Moq

Writing unit tests can be difficult, time-consuming, and slow when you can’t isolate the classes you want to test from the rest of the system. In this course, Mocking in .NET Core Unit Tests with Moq: Getting Started, you’ll learn how to create mocks and use them as dependencies to the classes you want to test. First, you’ll discover how to configure mocked methods and properties to return specific values. Next, you’ll cover how to perform behavior/interaction testing. Finally, you’ll explore how to set up mocked exceptions and events. When you’re finished with this course, you’ll have the necessary knowledge to use Moq to unit test your classes in isolation by creating and using mock objects.

Setup the Project

Let’s create a sample web API Project with basic crud operations using EF Core code first approach.

Fig-1
Fig-2

Since .Net 5.0 was installed on my machine so that I am going with the latest template we can choose what version we are comfortable with.

Fig-3

Create the Model Folder and inside will configure the Model class and DbContext for the EntityFramework Core Code First approach setup.

Fig-4

Employee.cs

AppDbContext.cs

Let’s set up the connection string to perform the code first operations.

appsettings.json

Startup.cs

Create the tables by using the below commands in the console.

Step 1

To create a migration script

PM> Add-Migration ‘Initial’

Step 2

To execute the script in SQL Db

PM> update-database

Create a Services folder where we perform our business logic for all the operations.

Fig-5

EmployeeService.cs

IEmployeeService.cs

Define these services in Startup. cs file which I have already highlighted in the above-mentioned startup.cs file.

Create API methods for those services in the controller class.

EmployeeController.cs

Let us create another testing project inside this solution project where we can write test cases for those functions

  • Right-click on the Solution
  • Click on Add — New project
  • Search for X-Unit Test project.
Fig-6
Fig-7

Choose the target framework the same as where we have used it in our API project.

Fig-8

Install the Moq package inside this unit test project.

Fig-9

Create a class inside this Test project to define all our respective test cases but before that, we have to insert data into the table which we have created. Open the SQL Server and insert dummy data to the employee table.

EmployeeTest.cs

setting up the mock for our API business services under the controller level to check the result and compare with user-defined values.

we can debug the test cases to check the output in running mode.

Run all the test cases to verify whether they are passed or failed.

  • Click on View in the Top left
  • Click on Test explorer.
Fig-10

In the above image, we can see all our test cases are passed and their time duration as well

Hope this article helps you in understanding unit testing using the Mock object.

Keep learning ……!

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Handling Environments with Typeform

Defining the User Schema and Migration

Mphasis Off Campus Drive | Freshers | Trainee Associate Engineer | BE/ B.Tech/ MCA

Video Editing on Linux — Action Cam SCUBA Diving Videos

Wen Fixed-Rate ETH 2.0 Staking?

Discovering Systemic Sources of Problems

images/integrating-13.1-1-net.png

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jay Krishna Reddy

Jay Krishna Reddy

Full Stack Developer | Top Writer ✍🏻 Medium | C# Corner MVP 🏅| Cloud Enthusiast ☁️

More from Medium

Unit Tests a .NET 6 project with Azure Pipelines DotNetCoreCLI@2

GitHub Gist focus image

CQRS Pattern Implementation

Multiple Request/Response examples for Swagger UI in ASP.NET core

Inversion of Control IoC