Achraf Ben Alaya

Achraf Ben Alaya

  • NA
  • 134
  • 4.2k

Unit Test WebAPi

May 1 2018 8:25 AM
 I have this controller and I want to create a unit test for it , I have a small experience with unit testing but now I want to learn it , I'm using Dto here and automapper .
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Data.Entity.Infrastructure;  
  6. using System.Linq;  
  7. using System.Net;  
  8. using System.Net.Http;  
  9. using System.Web.Http;  
  10. using System.Web.Http.Description;  
  11. using AutoMapper;  
  12. using WebAPiBackend.Dto;  
  13. using WebAPiBackend.Models;  
  14.   
  15. namespace WebAPiBackend.Controllers  
  16. {  
  17.     [AllowAnonymous]  
  18.     public class SpecialitiesController : ApiController  
  19.     {  
  20.         private ApplicationDbContext db = new ApplicationDbContext();  
  21.   
  22.         //// GET: api/Specialities  
  23.         //public IQueryable GetSpecialities()  
  24.         //{  
  25.         //    return db.Specialities;  
  26.         //}
  27.         //get with Dto Done  
  28.         public IEnumerable GetSpecialities()  
  29.         {  
  30.             var specQuery = db.Specialities;  
  31.   
  32.             return specQuery  
  33.                 .ToList()  
  34.                 .Select(Mapper.Map);  
  35.         }
  36.         //DTO  
  37.         // GET: api/Specialities/5  
  38.         [ResponseType(typeof(Specialities))]  
  39.         public IHttpActionResult GetSpecialities(int id)  
  40.         {  
  41.             //Specialities specialities = db.Specialities.Find(id);  
  42.             Specialities specialities = db.Specialities.SingleOrDefault(c => c.SpecialtyId == id);
  43.             if (specialities == null)  
  44.             {  
  45.                 return NotFound();  
  46.             }  
  47.             return Ok(Mapper.Map(specialities));  
  48.   
  49.             //return Ok(specialities);  
  50.         }
  51.         //DTO  
  52.         // PUT: api/Specialities/5  
  53.         [ResponseType(typeof(void))]  
  54.         public IHttpActionResult PutSpecialities(int id, SpecialitiesDto specialitiesDto)  
  55.         {  
  56.             if (!ModelState.IsValid)  
  57.             {  
  58.                 return BadRequest(ModelState);  
  59.             }
  60.             var speciinDB = db.Specialities.SingleOrDefault(c => c.SpecialtyId == id);  
  61.             if (speciinDB == null)  
  62.                 return NotFound();  
  63.             Mapper.Map(specialitiesDto, speciinDB);  
  64.             db.SaveChanges();  
  65.             return Ok();
  66.         }  
  67.   
  68.         // POST: api/Specialities  
  69.         [ResponseType(typeof(Specialities))]  
  70.         public IHttpActionResult PostSpecialities(SpecialitiesDto specialitiesDto)  
  71.         {  
  72.             if (!ModelState.IsValid)  
  73.             {  
  74.                 return BadRequest(ModelState);  
  75.             }
  76.             //addingforDTo  
  77.             var specialDTO = Mapper.Map(specialitiesDto);  
  78.   
  79.             db.Specialities.Add(specialDTO);  
  80.             db.SaveChanges();  
  81.             specialitiesDto.SpecialtyId = specialDTO.SpecialtyId;  
  82.             return CreatedAtRoute("DefaultApi"new { id = specialDTO.SpecialtyId }, specialitiesDto);  
  83.         }  
  84.   
  85.         // DELETE: api/Specialities/5  
  86.         [ResponseType(typeof(Specialities))]  
  87.         public IHttpActionResult DeleteSpecialities(int id)  
  88.         {  
  89.             Specialities specialities = db.Specialities.Find(id);  
  90.             if (specialities == null)  
  91.             {  
  92.                 return NotFound();  
  93.             }
  94.             //db.Specialities.Remove(specialities);  
  95.             specialities.IsDeleted = true;  
  96.             db.SaveChanges();
  97.             return Ok(specialities);  
  98.         }  
  99.   
  100.         protected override void Dispose(bool disposing)  
  101.         {  
  102.             if (disposing)  
  103.             {  
  104.                 db.Dispose();  
  105.             }  
  106.             base.Dispose(disposing);  
  107.         }  
  108.   
  109.         private bool SpecialitiesExists(int id)  
  110.         {  
  111.             return db.Specialities.Count(e => e.SpecialtyId == id) > 0;  
  112.         }  
  113.     }  
  114. }  

Answers (1)