Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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));
        }