public async Task E_Possivel_Invocar_a_Controller_Create() { var serviceMock = new Mock <ICourseService>(); var nome = Faker.Name.FullName(); serviceMock.Setup(m => m.Post(It.IsAny <CourseDtoCreate>())).ReturnsAsync( new CourseDtoCreateResult { Id = Guid.NewGuid(), Name = nome } ); _controller = new CoursesController(serviceMock.Object); Mock <IUrlHelper> url = new Mock <IUrlHelper>(); url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000"); _controller.Url = url.Object; var CourseDtoCreate = new CourseDtoCreate { Name = nome }; var result = await _controller.Post(CourseDtoCreate); Assert.True(result is CreatedResult); var resultValue = ((CreatedResult)result).Value as CourseDtoCreateResult; Assert.NotNull(resultValue); Assert.Equal(CourseDtoCreate.Name, resultValue.Name); }
public async Task <CourseDtoCreateResult> Post(CourseDtoCreate Course) { var model = _mapper.Map <CourseModel>(Course); var entity = _mapper.Map <CourseEntity>(model); var result = await _repository.InsertAsync(entity); return(_mapper.Map <CourseDtoCreateResult>(result)); }
public CourseTestes() { IdUsuario = Guid.NewGuid(); NameCourse = Faker.Name.FullName(); NameCourseAlterado = Faker.Name.FullName(); for (int i = 0; i < 10; i++) { var dto = new CourseDto() { Id = Guid.NewGuid(), Name = Faker.Name.FullName() }; listaCourseDto.Add(dto); } CourseDto = new CourseDto { Id = IdUsuario, Name = NameCourse }; CourseDtoCreate = new CourseDtoCreate { Name = NameCourse }; CourseDtoCreateResult = new CourseDtoCreateResult { Id = IdUsuario, Name = NameCourse, CreateAt = DateTime.UtcNow }; CourseDtoUpdate = new CourseDtoUpdate { Id = IdUsuario, Name = NameCourseAlterado }; CourseDtoUpdateResult = new CourseDtoUpdateResult { Id = IdUsuario, Name = NameCourseAlterado, UpdateAt = DateTime.UtcNow }; }
public async Task <ActionResult> Post([FromBody] CourseDtoCreate course) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _service.Post(course); if (result != null) { return(Created(new Uri(Url.Link("GetWithIdCourse", new { id = result.Id })), result)); } else { return(BadRequest()); } } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task E_Possivel_Realizar_Crud_Course() { _name = Faker.Name.First(); _email = Faker.Internet.Email(); var courseDto = new CourseDtoCreate() { Name = _name }; //Post var response = await PostJsonAsync(courseDto, $"{hostApi}courses", client); var postResult = await response.Content.ReadAsStringAsync(); var registroPost = JsonConvert.DeserializeObject <CourseDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(_name, registroPost.Name); Assert.True(registroPost.Id != default(Guid)); //Get All response = await client.GetAsync($"{hostApi}courses"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonResult = await response.Content.ReadAsStringAsync(); var listaFromJson = JsonConvert.DeserializeObject <IEnumerable <CourseDto> >(jsonResult); Assert.NotNull(listaFromJson); Assert.True(listaFromJson.Count() > 0); Assert.True(listaFromJson.Where(r => r.Id == registroPost.Id).Count() == 1); var updateCourseDto = new CourseDtoUpdate() { Id = registroPost.Id, Name = Faker.Name.FullName() }; //PUT var stringContent = new StringContent(JsonConvert.SerializeObject(updateCourseDto), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}courses", stringContent); jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <CourseDtoUpdateResult>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotEqual(registroPost.Name, registroAtualizado.Name); //GET Id response = await client.GetAsync($"{hostApi}courses/{registroAtualizado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <CourseDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(registroSelecionado.Name, registroAtualizado.Name); //DELETE response = await client.DeleteAsync($"{hostApi}courses/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); //GET ID depois do DELETE response = await client.GetAsync($"{hostApi}courses/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }