TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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 .
using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Data.Entity;
using
System.Data.Entity.Infrastructure;
using
System.Linq;
using
System.Net;
using
System.Net.Http;
using
System.Web.Http;
using
System.Web.Http.Description;
using
AutoMapper;
using
WebAPiBackend.Dto;
using
WebAPiBackend.Models;
namespace
WebAPiBackend.Controllers
{
[AllowAnonymous]
public
class
SpecialitiesController : ApiController
{
private
ApplicationDbContext db =
new
ApplicationDbContext();
//// GET: api/Specialities
//public IQueryable
GetSpecialities()
//{
// return db.Specialities;
//}
//get with Dto Done
public
IEnumerable
GetSpecialities()
{
var specQuery = db.Specialities;
return
specQuery
.ToList()
.Select(Mapper.Map
);
}
//DTO
// GET: api/Specialities/5
[ResponseType(
typeof
(Specialities))]
public
IHttpActionResult GetSpecialities(
int
id)
{
//Specialities specialities = db.Specialities.Find(id);
Specialities specialities = db.Specialities.SingleOrDefault(c => c.SpecialtyId == id);
if
(specialities ==
null
)
{
return
NotFound();
}
return
Ok(Mapper.Map
(specialities));
//return Ok(specialities);
}
//DTO
// PUT: api/Specialities/5
[ResponseType(
typeof
(
void
))]
public
IHttpActionResult PutSpecialities(
int
id, SpecialitiesDto specialitiesDto)
{
if
(!ModelState.IsValid)
{
return
BadRequest(ModelState);
}
var speciinDB = db.Specialities.SingleOrDefault(c => c.SpecialtyId == id);
if
(speciinDB ==
null
)
return
NotFound();
Mapper.Map(specialitiesDto, speciinDB);
db.SaveChanges();
return
Ok();
}
// POST: api/Specialities
[ResponseType(
typeof
(Specialities))]
public
IHttpActionResult PostSpecialities(SpecialitiesDto specialitiesDto)
{
if
(!ModelState.IsValid)
{
return
BadRequest(ModelState);
}
//addingforDTo
var specialDTO = Mapper.Map
(specialitiesDto);
db.Specialities.Add(specialDTO);
db.SaveChanges();
specialitiesDto.SpecialtyId = specialDTO.SpecialtyId;
return
CreatedAtRoute(
"DefaultApi"
,
new
{ id = specialDTO.SpecialtyId }, specialitiesDto);
}
// DELETE: api/Specialities/5
[ResponseType(
typeof
(Specialities))]
public
IHttpActionResult DeleteSpecialities(
int
id)
{
Specialities specialities = db.Specialities.Find(id);
if
(specialities ==
null
)
{
return
NotFound();
}
//db.Specialities.Remove(specialities);
specialities.IsDeleted =
true
;
db.SaveChanges();
return
Ok(specialities);
}
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing)
{
db.Dispose();
}
base
.Dispose(disposing);
}
private
bool
SpecialitiesExists(
int
id)
{
return
db.Specialities.Count(e => e.SpecialtyId == id) > 0;
}
}
}
Reply
Answers (
1
)
not showing the image properly
Unclosed quotation mark after the character string ''. error