Unit Testing In ASP.NET Core Web API

Step by step implementation of ASP.NET Core web API Unit testing using xUnit.

Pritomsarkar
.Net Programming
6 min readJul 10, 2021

--

Overview

A unit test is a code you can write to test your application code. Your web application will not have any knowledge of your test project, but your test project will need to have a dependency on the app project that it’s testing. So having your code well-tested helps you prevent bugs from getting into production. In this article, you’ll learn how to unit test an ASP.NET Core web API and an MVC application.

Prerequisites

I expect you to have knowledge of object-oriented programming concepts in C#. I assume you know .NET Core API concepts especially the MVC pattern also.

To code along with me, you will have to install the .NET Core 3.1, as well as Visual Studio. You can also use another IDE instead of a visual studio. You can find a link to the Github repository end of this article.

Environment Setup

Before the write any unit testing code, we have to know what project is actually used for testing. Now I give some code which is actually we will test in this article.

Book.cs
BookService.cs
IBookService.cs
BooksController.cs

This small project we are going to testing.

Set up web API testing project

Let’s go to the visual studio and setup our created project in testing mode. let’s right-click on New Solution in our created project. Search here for “xUnit” and then choose the xUnit test project .NET core option. Then click the next button. We’re going to provide a name for this project so that is going to be libraryAPI.Test and then click the create button like below.

Now Name this project libraryAPI.Test then click to Create button.

So, our unit testing project was created.

The first thing that we need to do is that we need to create a reference to the project that we want to test. So, right-click on the dependencies of the libraryAPI.Test project. and go to add a reference. In here, check the Library.API project. And then click the “OK” button.

If you Expand the Dependencies, You will see our project.

When we created the unit testing project, a file was created by default. Let us modify the file names, so right-click on UnitTest1.cs file and rename this file BooksControllerTest.cs and then press enter. Yes, we want to modify the class name.

And we see the name in here was updated. Now, we need to set up the resources that we are going to use for the unit test. And that’s a reference to Books Controller.

We will follow a simple rule for Unit-Testing.

Arrange

Act

Assert

Arrange mean that we need to arrange or set up the necessary parameters for the test.

Act means just do the action which might be calling a method, calling a controller.

Assert means just evaluate the result.

Now we will test our BooksController’s Get() method using BooksControllerTest’s GetAllTest() method.

Above code, when you will want to set up the resources, you may use the constructor. So, first, we defined the references. I’ll write the service to be the reference to the new Book Service class. And the controller is going to be a new Books Controller which takes as a parameter the _service. Because if we go to our controller, We see that it takes an I book Service parameter. So if we go to Books Controller we see that the first API endpoint is the Get. It just gets all the items from our service and returns them using the Ok method.

we are not going to pass any parameters in our BooksControllerTest file, because the Get method in BooksController doesn’t take any parameters, it just returns all the items. We are using here GetAllTest() method for Testing our Get() method in our BooksController.

I already said We are not going to pass any parameters so,Arrange is null in this case. Now Act part, here we called the Controller’s Get method and store in the variable its name result. Now time to start evaluating the result.

The first thing that we checked response type is an OkObjectResult, then once we know that the API endpoint returned an OkObject, we checked the result type, Is it a list of books? yes, it is. Then at the end, we checked the number of books that were returned. So we successfully tested our BooksController’s Get method using BooksControllerTest’s GetAllTest method.So now we have to build our solution and then Test.

So,we already have seen all test case is passed successfully.

Now we will test our BooksController’s Get by id method using BooksControllerTest’s GetBookByIdTest() method.

Above code,is tested for Get by id method in BookController.If you will check this method in BookController,if book(item) exits,return ok and it has a parameter(item) the book(item) and if not exits,return not found.

When we testing,we tested both cases for Ok and Notfound.GetBookByIdTest method,here used two parameter guid1 and guid2.these parameter value comes from InlineData which above our GetBookByIdTest method.So first InlineData value is correct and second InlineData is wrong.we used wrong value because of NotFound test,and correct value for Ok test.Now time to test our code.

Test case run process is same like my above discussion.just diffrent is which you want test just select this and right click then run.In this case we test our GetBookByIdTest method.So select GetBookByIdTest then right click then run.I discuss there details,because i will not discuss from now this Test case run fact.because all is take same process for run.

So,Test is successfully passed.

Now we will test our BooksController’s Post method using BooksControllerTest’s AddBookTest() method.

Above code,we are tested two cases,Ok return and BadRequest also Modelstatevalidition.Now time to test our code.

So,test is successfully passed.

Now we will test our BooksController’s Remove method using BooksControllerTest’s RemoveBookByIdTest() method.

Above code,mainly we tested our code NotFound and Ok.also we tested count item etc.Now time to test our code.

So everything works as expected.

Conclusion

This article hasn’t covered all aspects of a professional Unit testing, but you learned some of the basics. you can download this source code in my Github Repository. hope you guys enjoy this article.

--

--