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.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,17 +15,19 @@ namespace Microsoft.eShopWeb.Controllers
private readonly IHostingEnvironment _env;
private readonly ICatalogService _catalogService;
private readonly IImageService _imageService;
private readonly ILogger<CatalogController> _logger;
public CatalogController(IHostingEnvironment env,
ICatalogService catalogService,
IImageService imageService)
IImageService imageService,
ILogger<CatalogController> logger)
{
_env = env;
_catalogService = catalogService;
_imageService = imageService;
_logger = logger;
}
// GET: /<controller>/
public async Task<IActionResult> 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");

View File

@@ -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<IImageService> _mockImageService = new Mock<IImageService>();
private Mock<ILogger<CatalogController>> _mockLogger = new Mock<ILogger<CatalogController>>();
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<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();
}
}
}