Looking to unit test logging but blocked by extension method currently.

This commit is contained in:
Steve Smith
2017-04-28 20:27:18 -04:00
parent 5dbbc4c791
commit 6f908bb8e5
2 changed files with 41 additions and 15 deletions

View File

@@ -3,10 +3,10 @@ using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using ApplicationCore.Interfaces; using ApplicationCore.Interfaces;
using ApplicationCore.Exceptions; using ApplicationCore.Exceptions;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.Controllers namespace Microsoft.eShopWeb.Controllers
{ {
@@ -15,17 +15,19 @@ namespace Microsoft.eShopWeb.Controllers
private readonly IHostingEnvironment _env; private readonly IHostingEnvironment _env;
private readonly ICatalogService _catalogService; private readonly ICatalogService _catalogService;
private readonly IImageService _imageService; private readonly IImageService _imageService;
private readonly ILogger<CatalogController> _logger;
public CatalogController(IHostingEnvironment env, public CatalogController(IHostingEnvironment env,
ICatalogService catalogService, ICatalogService catalogService,
IImageService imageService) IImageService imageService,
ILogger<CatalogController> logger)
{ {
_env = env; _env = env;
_catalogService = catalogService; _catalogService = catalogService;
_imageService = imageService; _imageService = imageService;
_logger = logger;
} }
// GET: /<controller>/ // GET: /<controller>/
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page) public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
{ {
@@ -66,6 +68,7 @@ namespace Microsoft.eShopWeb.Controllers
} }
catch (CatalogImageMissingException ex) catch (CatalogImageMissingException ex)
{ {
_logger.LogWarning($"No image found for id: {id}");
return NotFound(); return NotFound();
} }
return File(imageBytes, "image/png"); return File(imageBytes, "image/png");

View File

@@ -3,6 +3,7 @@ using ApplicationCore.Interfaces;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.Controllers; using Microsoft.eShopWeb.Controllers;
using Microsoft.Extensions.Logging;
using Moq; using Moq;
using Xunit; using Xunit;
@@ -11,22 +12,21 @@ namespace UnitTests
public class CatalogControllerGetImage public class CatalogControllerGetImage
{ {
private Mock<IImageService> _mockImageService = new Mock<IImageService>(); private Mock<IImageService> _mockImageService = new Mock<IImageService>();
private Mock<ILogger<CatalogController>> _mockLogger = new Mock<ILogger<CatalogController>>();
private CatalogController _controller; private CatalogController _controller;
private int _testImageId = 123; private int _testImageId = 123;
private byte[] _testBytes = { 0x01, 0x02, 0x03 }; private byte[] _testBytes = { 0x01, 0x02, 0x03 };
public CatalogControllerGetImage() public CatalogControllerGetImage()
{ {
_controller = new CatalogController(null, null, _mockImageService.Object); _controller = new CatalogController(null, null, _mockImageService.Object,
_mockLogger.Object);
} }
[Fact] [Fact]
public void CallsImageServiceWithId() public void CallsImageServiceWithId()
{ {
_mockImageService SetupImageWithTestBytes();
.Setup(i => i.GetImageBytesById(_testImageId))
.Returns(_testBytes)
.Verifiable();
_controller.GetImage(_testImageId); _controller.GetImage(_testImageId);
_mockImageService.Verify(); _mockImageService.Verify();
@@ -35,9 +35,7 @@ namespace UnitTests
[Fact] [Fact]
public void ReturnsFileResultWithBytesGivenSuccess() public void ReturnsFileResultWithBytesGivenSuccess()
{ {
_mockImageService SetupImageWithTestBytes();
.Setup(i => i.GetImageBytesById(_testImageId))
.Returns(_testBytes);
var result = _controller.GetImage(_testImageId); var result = _controller.GetImage(_testImageId);
@@ -48,13 +46,38 @@ namespace UnitTests
[Fact] [Fact]
public void ReturnsNotFoundResultGivenImageMissingException() public void ReturnsNotFoundResultGivenImageMissingException()
{ {
_mockImageService SetupMissingImage();
.Setup(i => i.GetImageBytesById(_testImageId))
.Throws(new CatalogImageMissingException("missing image"));
var result = _controller.GetImage(_testImageId); var result = _controller.GetImage(_testImageId);
var actionResult = Assert.IsType<NotFoundResult>(result); var actionResult = Assert.IsType<NotFoundResult>(result);
} }
[Fact]
public void LogsWarningGivenImageMissingException()
{
SetupMissingImage();
_mockLogger.Setup(l => l.LogWarning(It.IsAny<string>()))
.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();
}
} }
} }