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:

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:

Step 2

Now in the TestApp project add the model class.

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 { get; set; }
  10. public string Name { get; set; }
  11. public decimal Cost { set; get; }
  12. }
  13. }
Step 3

Now add the Web API2 Controller.

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.

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.