public async Task Post_400_Exception() { // arrage var currency = new CurrencyPostModel { Name = "usd", PurchasePrice = 10, SellingPrice = 20 }; rateService.Setup(x => x.AddCurrencyAsync(It.IsAny <Currency>())) .Throws <Exception>() .Verifiable(); // act var action = await controller.Post(currency); // assert Assert.IsInstanceOfType(action, typeof(BadRequestObjectResult)); var result = action as BadRequestObjectResult; Assert.AreEqual(result.StatusCode, 400); }
public async Task Post_Ok() { // arrage var currency = new CurrencyPostModel { Name = "usd", PurchasePrice = 10, SellingPrice = 20 }; rateService.Setup(x => x.AddCurrencyAsync(It.IsAny <Currency>())) .Returns(Task.CompletedTask) .Verifiable(); // act var action = await controller.Post(currency); // assert Assert.IsInstanceOfType(action, typeof(OkResult)); var result = action as OkResult; Assert.AreEqual(result.StatusCode, 200); rateService.Verify(); }
public async Task <IActionResult> Post([FromBody] CurrencyPostModel postModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var currency = mapper.Map <CurrencyPostModel, Currency>(postModel); await rateService.AddCurrencyAsync(currency); return(Ok()); } catch (Exception ex) { return(BadRequest(new ErrorResponse { Message = ex.Message })); } }