public async Task Create_ShouldWork() { //Arrange using var client = new TestClientProvider().Client; var newUser = new UserPostRequestDto() { FirstName = "TestFirstName", MiddleName = "TestMiddleName", LastName = "TestLastName", EmailAddress = "*****@*****.**", PhoneNumber = "(555) 555-1234" }; var stringContent = new StringContent(JsonConvert.SerializeObject(newUser), Encoding.UTF8, "application/json"); //Act var response = await client.PostAsync($"/user/", stringContent); //Assert response.EnsureSuccessStatusCode(); var stringData = response.Content.ReadAsStringAsync().Result; Assert.Equal(HttpStatusCode.OK, response.StatusCode); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var newId = System.Text.Json.JsonSerializer.Deserialize <UserPostResponseDto>(stringData, options); Assert.NotNull(newId); }
public async Task <ActionResult <UserPostResponseDto> > Post( [Required, FromBody] UserPostRequestDto userDto) { try { var userMap = _mapper.Map <User>(userDto); var user = await _mediator.Send(new CreateUserCommand { User = userMap }); return(new UserPostResponseDto { Id = user.Id }); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task Create_ValidationError() { //Arrange using var client = new TestClientProvider().Client; var newUser = new UserPostRequestDto() { FirstName = "TestFirstName", MiddleName = "TestMiddleName", LastName = "TestLastName", EmailAddress = "TestEmailAddress~yahoo.com", PhoneNumber = "*****@*****.**" }; var stringContent = new StringContent(JsonConvert.SerializeObject(newUser), Encoding.UTF8, "application/json"); //Act var response = await client.PostAsync($"/user/", stringContent); //Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }