From 6f908bb8e527e9360944f931dde9b61dd76a596a Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 28 Apr 2017 20:27:18 -0400 Subject: [PATCH] Looking to unit test logging but blocked by extension method currently. --- src/Web/Controllers/CatalogController.cs | 11 +++-- .../Controllers/CatalogControllerGetImage.cs | 45 ++++++++++++++----- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/Web/Controllers/CatalogController.cs b/src/Web/Controllers/CatalogController.cs index fa7bb18..eb64454 100644 --- a/src/Web/Controllers/CatalogController.cs +++ b/src/Web/Controllers/CatalogController.cs @@ -3,10 +3,10 @@ using Microsoft.eShopWeb.ViewModels; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System; -using System.IO; using System.Threading.Tasks; using ApplicationCore.Interfaces; using ApplicationCore.Exceptions; +using Microsoft.Extensions.Logging; namespace Microsoft.eShopWeb.Controllers { @@ -15,16 +15,18 @@ namespace Microsoft.eShopWeb.Controllers private readonly IHostingEnvironment _env; private readonly ICatalogService _catalogService; private readonly IImageService _imageService; + private readonly ILogger _logger; public CatalogController(IHostingEnvironment env, ICatalogService catalogService, - IImageService imageService) + IImageService imageService, + ILogger logger) { _env = env; _catalogService = catalogService; _imageService = imageService; - } - + _logger = logger; + } // GET: // public async Task Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page) @@ -66,6 +68,7 @@ namespace Microsoft.eShopWeb.Controllers } catch (CatalogImageMissingException ex) { + _logger.LogWarning($"No image found for id: {id}"); return NotFound(); } return File(imageBytes, "image/png"); diff --git a/tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs b/tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs index 6bcdb4e..a16b695 100644 --- a/tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs +++ b/tests/UnitTests/Web/Controllers/CatalogControllerGetImage.cs @@ -3,6 +3,7 @@ using ApplicationCore.Interfaces; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopWeb.Controllers; +using Microsoft.Extensions.Logging; using Moq; using Xunit; @@ -11,22 +12,21 @@ namespace UnitTests public class CatalogControllerGetImage { private Mock _mockImageService = new Mock(); + private Mock> _mockLogger = new Mock>(); private CatalogController _controller; private int _testImageId = 123; private byte[] _testBytes = { 0x01, 0x02, 0x03 }; public CatalogControllerGetImage() { - _controller = new CatalogController(null, null, _mockImageService.Object); + _controller = new CatalogController(null, null, _mockImageService.Object, + _mockLogger.Object); } [Fact] public void CallsImageServiceWithId() { - _mockImageService - .Setup(i => i.GetImageBytesById(_testImageId)) - .Returns(_testBytes) - .Verifiable(); + SetupImageWithTestBytes(); _controller.GetImage(_testImageId); _mockImageService.Verify(); @@ -35,9 +35,7 @@ namespace UnitTests [Fact] public void ReturnsFileResultWithBytesGivenSuccess() { - _mockImageService - .Setup(i => i.GetImageBytesById(_testImageId)) - .Returns(_testBytes); + SetupImageWithTestBytes(); var result = _controller.GetImage(_testImageId); @@ -48,13 +46,38 @@ namespace UnitTests [Fact] public void ReturnsNotFoundResultGivenImageMissingException() { - _mockImageService - .Setup(i => i.GetImageBytesById(_testImageId)) - .Throws(new CatalogImageMissingException("missing image")); + SetupMissingImage(); var result = _controller.GetImage(_testImageId); var actionResult = Assert.IsType(result); } + + [Fact] + public void LogsWarningGivenImageMissingException() + { + SetupMissingImage(); + _mockLogger.Setup(l => l.LogWarning(It.IsAny())) + .Verifiable(); + + _controller.GetImage(_testImageId); + + _mockLogger.Verify(); + } + + private void SetupMissingImage() + { + _mockImageService + .Setup(i => i.GetImageBytesById(_testImageId)) + .Throws(new CatalogImageMissingException("missing image")); + } + + private void SetupImageWithTestBytes() + { + _mockImageService + .Setup(i => i.GetImageBytesById(_testImageId)) + .Returns(_testBytes) + .Verifiable(); + } } }