Refactoring and Adding Tests (#28)
* Introducing repository and refactoring services. Changing entities to use int keys everywhere. * Refactoring application services to live in web project and only reference repositories, not EF contexts. * Cleaning up implementations * Moving logic out of CatalogController Moving entity knowledge out of viewmodels. * Implementing specification includes better for catalogservice * Cleaning up and adding specification unit tests
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.2.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.2" />
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.2.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||
<PackageReference Include="Moq" Version="4.7.49" />
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using ApplicationCore.Specifications;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
public class BasketWithItems
|
||||
{
|
||||
private int _testBasketId = 123;
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||
|
||||
var result = GetTestBasketCollection()
|
||||
.AsQueryable()
|
||||
.FirstOrDefault(spec.Criteria);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_testBasketId, result.Id);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfIdNotPresent()
|
||||
{
|
||||
int badId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badId);
|
||||
|
||||
Assert.False(GetTestBasketCollection()
|
||||
.AsQueryable()
|
||||
.Any(spec.Criteria));
|
||||
}
|
||||
|
||||
public List<Basket> GetTestBasketCollection()
|
||||
{
|
||||
return new List<Basket>()
|
||||
{
|
||||
new Basket() { Id = 1 },
|
||||
new Basket() { Id = 2 },
|
||||
new Basket() { Id = _testBasketId }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,15 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
|
||||
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
|
||||
<PackageReference Include="Moq" Version="4.7.49" />
|
||||
<PackageReference Include="xunit" Version="2.2.0" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.2.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ApplicationCore\ApplicationCore.csproj" />
|
||||
<ProjectReference Include="..\..\src\Web\Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
using ApplicationCore.Exceptions;
|
||||
using ApplicationCore.Interfaces;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.Controllers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
//public class CatalogControllerGetImage
|
||||
//{
|
||||
// private Mock<IImageService> _mockImageService = new Mock<IImageService>();
|
||||
// private Mock<IAppLogger<CatalogController>> _mockLogger = new Mock<IAppLogger<CatalogController>>();
|
||||
// private CatalogController _controller;
|
||||
// private int _testImageId = 123;
|
||||
// private byte[] _testBytes = { 0x01, 0x02, 0x03 };
|
||||
|
||||
// public CatalogControllerGetImage()
|
||||
// {
|
||||
// _controller = new CatalogController(null, null, _mockImageService.Object,
|
||||
// _mockLogger.Object);
|
||||
// }
|
||||
|
||||
// [Fact]
|
||||
// public void CallsImageServiceWithId()
|
||||
// {
|
||||
// SetupImageWithTestBytes();
|
||||
|
||||
// _controller.GetImage(_testImageId);
|
||||
// _mockImageService.Verify();
|
||||
// }
|
||||
|
||||
// [Fact]
|
||||
// public void ReturnsFileResultWithBytesGivenSuccess()
|
||||
// {
|
||||
// SetupImageWithTestBytes();
|
||||
|
||||
// var result = _controller.GetImage(_testImageId);
|
||||
|
||||
// var fileResult = Assert.IsType<FileContentResult>(result);
|
||||
// var bytes = Assert.IsType<byte[]>(fileResult.FileContents);
|
||||
// }
|
||||
|
||||
// [Fact]
|
||||
// public void ReturnsNotFoundResultGivenImageMissingException()
|
||||
// {
|
||||
// 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();
|
||||
// }
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user