Skip to content

Commit

Permalink
Unit tests working; added logger adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Apr 30, 2017
1 parent 6f908bb commit dfe0106
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/ApplicationCore/Interfaces/IAppLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ApplicationCore.Interfaces
{
public interface IAppLogger<T>
{
void LogWarning(string message, params object[] args);
}
}
11 changes: 1 addition & 10 deletions src/ApplicationCore/Interfaces/IImageService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
using ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Security.Principal;
using System.Threading.Tasks;

namespace ApplicationCore.Interfaces
namespace ApplicationCore.Interfaces
{

public interface IImageService
{
byte[] GetImageBytesById(int id);
}



}
18 changes: 18 additions & 0 deletions src/Infrastructure/Logging/LoggerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ApplicationCore.Interfaces;
using Microsoft.Extensions.Logging;

namespace Infrastructure.Logging
{
public class LoggerAdapter<T> : IAppLogger<T>
{
private readonly ILogger<T> _logger;
public LoggerAdapter(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<T>();
}
public void LogWarning(string message, params object[] args)
{
_logger.LogWarning(message, args);
}
}
}
4 changes: 2 additions & 2 deletions src/Web/Controllers/CatalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class CatalogController : Controller
private readonly IHostingEnvironment _env;
private readonly ICatalogService _catalogService;
private readonly IImageService _imageService;
private readonly ILogger<CatalogController> _logger;
private readonly IAppLogger<CatalogController> _logger;

public CatalogController(IHostingEnvironment env,
ICatalogService catalogService,
IImageService imageService,
ILogger<CatalogController> logger)
IAppLogger<CatalogController> logger)
{
_env = env;
_catalogService = catalogService;
Expand Down
2 changes: 2 additions & 0 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.AspNetCore.Http;
using ApplicationCore.Interfaces;
using Infrastructure.FileSystem;
using Infrastructure.Logging;

namespace Microsoft.eShopWeb
{
Expand Down Expand Up @@ -68,6 +69,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<CatalogService>();
services.Configure<CatalogSettings>(Configuration);
services.AddSingleton<IImageService, LocalFileImageService>();
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
services.AddMvc();

_services = services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace UnitTests
public class CatalogControllerGetImage
{
private Mock<IImageService> _mockImageService = new Mock<IImageService>();
private Mock<ILogger<CatalogController>> _mockLogger = new Mock<ILogger<CatalogController>>();
private Mock<IAppLogger<CatalogController>> _mockLogger = new Mock<IAppLogger<CatalogController>>();
private CatalogController _controller;
private int _testImageId = 123;
private byte[] _testBytes = { 0x01, 0x02, 0x03 };
Expand Down

0 comments on commit dfe0106

Please sign in to comment.