ASP.NET Web API testing using NUnit framework

Hello

I am not intending to provide an introduction for this topic, you can download the solution that I uploaded at MSDN code samples at http://code.msdn.microsoft.com/ASPNET-Web-API-NUnit-ac687169

Please note that in above solution the Web API service returns DTO in Json format to client.

To concise the solution consists of ASP.NET Web API project and a test project for testing service controllers and service Http response using HttpClient.

complete source is available at above URL from MSDN code sample. Here is the actual implementation of the same, which is replicated.

Controllers testing

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6:  
   7: using ContactManager.Controllers;
   8: using ContactManager.Models.Repository;
   9: using NUnit.Framework;
  10: using System.Web.Http;
  11: using ContactManager.Models.Entities;
  12:  
  13: namespace ContactsNUnitTests
  14: {
  15:     /// <summary>
  16:     /// Contains NUnit test cases for ContactsController
  17:     /// </summary>
  18:     [TestFixture]
  19:     public class ContactsControllerTests
  20:     {
  21:         ContactsController contactsController;
  22:         ContactRepository repository;
  23:         
  24:         int count;
  25:         int contactId;
  26:         
  27:         [SetUp]
  28:         public void Setup()
  29:         {
  30:             //create an instance of contactRepository
  31:             repository = new ContactRepository();
  32:             
  33:             //Create an instance of controller by passing repository
  34:             contactsController = new ContactsController(repository);
  35:             
  36:             //Number of records
  37:             count = contactsController.Get().contacts.Count;
  38:             
  39:             //Pass contact ID and store the retrieved contact ID
  40:             contactId = contactsController.Get(1).contact.Id;
  41:         }
  42:  
  43:         [Test]
  44:         public void GetAllContacts()
  45:         {
  46:           Assert.IsTrue(count > 0);
  47:         }
  48:  
  49:         [Test]
  50:         public void GetContact()
  51:         {
  52:           Assert.IsTrue(contactId.Equals(1));
  53:         }
  54:     }
  55: }

Service response testing

Testing service Http response using HttpClient

   1: namespace ContactsNUnitTests.ServiceHttpResponse
   2: {
   3:     using System;
   4:     using System.Collections.Generic;
   5:     using System.Linq;
   6:     using System.Text;
   7:  
   8:     using NUnit.Framework;
   9:     using System.Net.Http;
  10:     using System.Configuration;
  11:     using System.Net;
  12:     using System.Net.Http.Headers;
  13:  
  14:     /// <summary>
  15:     /// Service Http Response tests
  16:     /// </summary>
  17:     public class HttpResponseTests
  18:     {
  19:         private HttpClient client;
  20:  
  21:         private HttpResponseMessage response;
  22:  
  23:         [SetUp]
  24:         public void SetUP()
  25:         {
  26:             client = new HttpClient();
  27:             
  28:             client.BaseAddress = new Uri(ConfigurationManager.AppSettings["serviceBaseUri"]);
  29:             response = client.GetAsync("contacts/get").Result;
  30:         }
  31:  
  32:         [Test]
  33:         public void GetResponseIsSuccess()
  34:         {
  35:             Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
  36:         }
  37:  
  38:  
  39:         [Test]
  40:         public void GetResponseIsJson()
  41:         {
  42:             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  43:             
  44:             Assert.AreEqual("application/json", response.Content.Headers.ContentType.MediaType);
  45:         }
  46:  
  47:         [Test]
  48:         public void GetAuthenticationStatus()
  49:         {
  50:             Assert.AreNotEqual(HttpStatusCode.Unauthorized,
  51:  response.StatusCode);
  52:  
  53:         }
  54:     }
  55: }

References:

http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api

http://www.asp.net/web-api/overview/testing-and-debugging

http://blogs.msdn.com/b/youssefm/archive/2013/01/28/writing-tests-for-an-asp-net-webapi-service.aspx

No Comments