Пример #1
0
        public async Task GetByIdAsync_WhenCalledWithInvalidId_Returns404()
        {
            _beerService.GetByIdAsync(_beer.BeerId).Throws(new KeyNotFoundException());

            var response = await _controller.GetByIdAsync(_beer.BeerId);

            var statusCodeResult = response.Result as StatusCodeResult;

            Assert.AreEqual((HttpStatusCode)statusCodeResult.StatusCode, HttpStatusCode.NotFound);
        }
Пример #2
0
 public async Task <ActionResult <Beer> > GetByIdAsync([FromRoute] int id)
 {
     try
     {
         _logger.Information($"Getting beer with id: {id}.");
         return(await _beerService.GetByIdAsync(id));
     }
     catch (KeyNotFoundException ex)
     {
         _logger.Warning(ex, $"Beer with id: {id} could not be found");
         return(NotFound());
     }
     catch (Exception ex)
     {
         _logger.Error(ex, $"Request to get beer with id: {id} failed.");
         return(StatusCode(500));
     }
 }