[HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> CreateProductAsync(ProductDTO objProduct) { if (ModelState.IsValid) { HttpClient client = new HttpClient { BaseAddress = new Uri("http://localhost:2090") }; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsJsonAsync<ProductDTO>("/api/Product/Create/{product}", objProduct); var ProductDetails = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { TempData["Message"] = ProductDetails; return View(); } ViewBag.MessageViewCreate = ProductDetails; return View(); } else { return View(); } }
[Fact] public async System.Threading.Tasks.Task CreateProductAsync_ReturnsViewForCreateProductAsync() { var mockHttp = new MockHttpMessageHandler(); var product = new ProductDTO { ProductName = "Product01", ProductPrice = 535 }; mockHttp.When(HttpMethod.Post, "http://localhost:2090//api/Product/Create/{product}") .Respond(HttpStatusCode.OK, "application/json",""); var client = new HttpClient(mockHttp); HttpResponseMessage response = await client.PostAsJsonAsync<ProductDTO>("/api/Product/Create/{product}", product); //var response = await client.PostAsJsonAsync<ProductDTO>("http://localhost:2090/api/Product/Create/{product}", product); var ProductDetails = await response.Content.ReadAsStringAsync(); var result = productController.CreateProductAsync(product); Assert.IsType<ViewResult>(result.Result); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)