public async Task Should_Return_UpdatedPet_Given_Pet_Existed_When_Successfully_Updated_Pet() { // given TestServer server = new TestServer(new WebHostBuilder().UseStartup <Startup>()); HttpClient client = server.CreateClient(); await client.DeleteAsync("petStore/pets"); var pet = new Pet("SHISHI", Animal.Dog, "RED", 12); string request = JsonConvert.SerializeObject(pet); var requestBody = new StringContent(request, Encoding.UTF8, "application/json"); await client.PostAsync("petStore/pet", requestBody); // when var updatedPetNamePrice = new PetNamePrice("SHISHI", 13); string requestForPatch = JsonConvert.SerializeObject(updatedPetNamePrice); var requestBodyForPatch = new StringContent(requestForPatch, Encoding.UTF8, "application/json"); var urI = "petStore/pets"; var patchResponse = await client.PatchAsync(urI, requestBodyForPatch); // then patchResponse.EnsureSuccessStatusCode(); var getResponse = await client.GetAsync($"petStore/petName/{updatedPetNamePrice.Name}"); var responseString = await getResponse.Content.ReadAsStringAsync(); var actualPet = JsonConvert.DeserializeObject <Pet>(responseString); Assert.Equal(updatedPetNamePrice.Price, actualPet.Price); }
public ActionResult <Pet> UpdatePriceOfPet(PetNamePrice petNamePriceModel) { var pet = pets.FirstOrDefault(pet => pet.Name == petNamePriceModel.Name); if (pet == null) { return(NotFound()); } pet.Price = petNamePriceModel.Price; return(Ok(pet)); }
public async Task Should_Return_Not_Found_Given_Pet_Not_Existed_When_Update_Pet_Price() { // given TestServer server = new TestServer(new WebHostBuilder().UseStartup <Startup>()); HttpClient client = server.CreateClient(); await client.DeleteAsync("petStore/pets"); var pet = new Pet("SHISHI", Animal.Dog, "RED", 12); string request = JsonConvert.SerializeObject(pet); var requestBody = new StringContent(request, Encoding.UTF8, "application/json"); await client.PostAsync("petStore/pet", requestBody); // when var updatedPetNamePrice = new PetNamePrice("Tony", 13); string requestForPatch = JsonConvert.SerializeObject(updatedPetNamePrice); var requestBodyForPatch = new StringContent(requestForPatch, Encoding.UTF8, "application/json"); var urI = "petStore/pets"; var patchResponse = await client.PatchAsync(urI, requestBodyForPatch); // then Assert.Equal(HttpStatusCode.NotFound, patchResponse.StatusCode); }