public async Task ReturnUnprocessableEntityResponseIfModelstateNotValid() { var studentToAdd = new StudentForCreateViewModel(); var response = await Client.PostAsJsonAsync <StudentForCreateViewModel>("/api/Student", studentToAdd); var responseString = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode); Assert.Contains("The Name field is required.", responseString); Assert.Contains("The Age field is required.", responseString); }
public async Task ReturnCreatedAtRouteResponseIfEverythingIsFine() { var studentToAdd = new StudentForCreateViewModel() { Name = "Steve Jobs", Age = 22, }; var response = await Client.PostAsJsonAsync <StudentForCreateViewModel>("/api/Student", studentToAdd); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); }
public async Task <ActionResult> Post([FromBody] StudentForCreateViewModel studentViewModelForCreate) { if (studentViewModelForCreate == null) { return(BadRequest("Request body is null.")); } var student = new Student(); student = _mapper.Map <Student>(studentViewModelForCreate); if (await _studentService.CreateAsync(student) > 0) { return(CreatedAtRoute("GetStudentById", new { Id = student.Id }, studentViewModelForCreate)); } return(StatusCode(500)); }