public void GetById_CheckForSingleStudentInRepository() { var repository = new FakeDbRepository<Student>(); var stud = new Student() { FirstName = "First", LastName = "Last", Age = 100, Grade = 1 }; repository.entities.Add(stud); var studExpected = new StudentModelDetails() { Name = "First Last", Age = 100, Grade = 1 }; var controller = new StudentsController(repository); var studentModel = controller.GetStudent(1); Assert.AreEqual(studExpected.Name, studentModel.Name); Assert.AreEqual(studExpected.Age, studentModel.Age); Assert.AreEqual(studExpected.Grade, studentModel.Grade); // Created Student should not have any assigned Mark Assert.IsTrue(studentModel.Marks.Count() == 0); }
public void PostEmptyStudent_CheckForStatusCode400() { var server = new FakeHTTPServer<Student>("http://localhost/"); var newStudent = new StudentModelDetails(); var response = server.CreatePostRequest("api/Students", newStudent); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void Post_CheckForStatusCode201() { var server = new FakeHTTPServer<Student>("http://localhost/"); var newStudent = new StudentModelDetails() { Name = "First Last", Age = 100, Grade = 1 }; var response = server.CreatePostRequest("api/Students", newStudent); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsNotNull(response.Content); }
// POST api/Students public HttpResponseMessage PostStudent(StudentModelDetails student) { if (ModelState.IsValid) { if (student == null || student.Name == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Wrong input data"); } string[] names = student.Name.Split(new char[] {' '}); string firstName = names[0]; string lastName = (names.Length > 1)?names[1]:""; var stud = new Student() { FirstName = firstName, LastName = lastName, Age = student.Age, Grade = student.Grade }; //db.Students.Add(stud); //db.SaveChanges(); repository.Add(stud); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, student); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = stud.StudentId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }