Unit Test in ASP.Net Web API2

Introduction

This article explains Unit Testing in Web API 2. Here we create a Web API application with a Unit Test Project. You can create a Unit Test Project or you can also add a Unit Test Project to an existing project.

Use the following procedure to create the Unit Test application.

Step 1

Create the application:

  • Start Visual Studio 2013.
  • From the Start window select "New Project".
  • Select "Installed" -> "Template"-> "Visual C#" -> "Web" and select the "ASP.NET Web Application".

    Create a Web Application

  • Click on the "OK" button.
  • From the ASP.NET project window select "Empty" and check "Web API" and select "Unit Test" project.

    Select Unit Test with Web API

  • Click on the "OK" button.

After creating the Unit Test Project you will see two projects added to the Solution Explorer.

Solution Explorer

If you want to add the Unit Test Project to an existing application then you need to createit  like this:

  • In the Solution Explorer.
  • Right-click on the Project and select "Add" -> "New Project".
  • From the New Project window select "Test" from the  left panel and then select Unit Test Project .

    Add New Project

  • Click on the "OK" button.

Step 2

Now in the TestApp project add the model class.

  • In the "Solution Explorer".
  • Right-click on the Model folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select "Class".

    Add a model Class

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace TestApp.Models  
  6. {  
  7.     public class Item  
  8.     {  
  9.         public int ID { getset; }  
  10.         public string Name { getset; }  
  11.         public decimal Cost { setget; }  
  12.     }  
  13. } 
Step 3

Now add the Web API2 Controller.

  • Right-click on the Controller folder then select "Add" -> "New Scaffolded Item".

    Select scaffolded Item

  • Then select "Web API2 Controller- Empty".

    Select Web APPI2 Controller

  • Change the name.

    Change name

  • Click on the "Add" button.

Add the following Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using TestApp.Models;  
  8. namespace TestApp.Controllers  
  9. {  
  10.     public class SampleItemController : ApiController  
  11.     {  
  12.         List<Item> items = new List<Item>();  
  13.         public SampleItemController() { }  
  14.         public SampleItemController(List<Item> items)  
  15.         {  
  16.             this.items = items;  
  17.         }  
  18.         public IEnumerable<Item> GetAllItems()  
  19.         {  
  20.             return items;  
  21.         }  
  22.         public IHttpActionResult GetItem(int id)  
  23.         {  
  24.             var item = items.FirstOrDefault((p) => p.ID == id);  
  25.             if (item == null)  
  26.             {  
  27.                 return NotFound();  
  28.             }  
  29.             return Ok(item);  
  30.         }  
  31.     }  
  32. } 
Step 4

Now add the Microsoft ASP.NET Web API2 Core package.

  • Right-click on the "TestApp.Tests" project.
  • Select "Manage NuGet Packages".

    Select Nuget Package Manager

  • In the Search box type "asp.net web api".

    Install Web API2 Core package

  • And install the "Web API2 Core" package.

Step 5

Now create the tests. We will see in the Solution Explorer that an empty UnitTest1.cs file in the TestApp.Tests Project is added automatically. In this file set the class name as "TestSampleItemController" and replace the code with the following:

Show UnitTest1.cs file 
  1. namespace TestApp.Tests  
  2. {  
  3.     [TestClass]  
  4.     public class TestSampleItemController  
  5.     {  
  6.         [TestMethod]  
  7.         public void GetAllItem_ShouldReturnAllItems()  
  8.         {  
  9.             var testItems = GetTestItems();  
  10.             var controller = new SampleItemController(testItems);  
  11.             var result = controller.GetAllItems() as List<Item>;  
  12.             Assert.AreEqual(testItems.Count, result.Count);  
  13.         }  
  14.         [TestMethod]  
  15.         public void GetItem_ShouldReturnCorrectItem()  
  16.         {  
  17.             var testItems = GetTestItems();  
  18.             var controller = new SampleItemController(testItems);  
  19.             var result = controller.GetItem(4) as OkNegotiatedContentResult<Item>;  
  20.             Assert.IsNotNull(result);  
  21.             Assert.AreEqual(testItems[3].Name, result.Content.Name);  
  22.         }  
  23.         [TestMethod]  
  24.         public void GetItem_ShouldNotFindItem()  
  25.         {  
  26.             var controller = new SampleItemController(GetTestItems());  
  27.             var result = controller.GetItem(999);  
  28.             Assert.IsInstanceOfType(result, typeof(NotFoundResult));  
  29.         }  
  30.         private List<Item> GetTestItems()  
  31.         {  
  32.             var testItems = new List<Item>();  
  33.             testItems.Add(new Item{ ID = 1, Name = "Test1", Cost = 1 });  
  34.             testItems.Add(new Item { ID = 2, Name = "Test2", Cost = 3.75M });  
  35.             testItems.Add(new Item { ID = 3, Name = "Test3", Cost = 16.99M });  
  36.             testItems.Add(new Item { ID = 4, Name = "Test4", Cost = 11.00M });  
  37.             return testItems;  
  38.         }  
  39.     }  
  40. }   

Step 6

Now we run test from the Menu Bar.

  • Go to Menu Bar select "Test" -> "Run" -> "All Tests".

    Run the Test

  • Open the Test Explorer window to see the results.

    RunTestWindow


Similar Articles