Adding additional unit tests (#406)
* Creating new test class for RemoveEmptyItems * Adding tests for AddItemToBasket in BasketService * Removing unused GetBasketItemCountAsync * Adding tests for BasketWithItemsSpecification * Adding CustomerORdersWithItemsSpecification tests * Adding CatalogFilterPaginatedSpecifciation tests * Adding CatalogItemsSpecification tests
This commit is contained in:
@@ -56,16 +56,6 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
||||
Assert.Equal(1, firstItem.Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveEmptyItems()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
||||
basket.RemoveEmptyItems();
|
||||
|
||||
Assert.Equal(0, basket.Items.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CantAddItemWithNegativeQuantity()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.BasketTests
|
||||
{
|
||||
public class BasketRemoveEmptyItems
|
||||
{
|
||||
private readonly int _testCatalogItemId = 123;
|
||||
private readonly decimal _testUnitPrice = 1.23m;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
[Fact]
|
||||
public void RemovesEmptyBasketItems()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(_testCatalogItemId, _testUnitPrice, 0);
|
||||
basket.RemoveEmptyItems();
|
||||
|
||||
Assert.Equal(0, basket.Items.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Moq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
{
|
||||
public class AddItemToBasket
|
||||
{
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
|
||||
public AddItemToBasket()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryGetByIdAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(basket);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||
|
||||
_mockBasketRepo.Verify(x => x.GetByIdAsync(It.IsAny<int>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryUpdateAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(basket);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||
|
||||
_mockBasketRepo.Verify(x => x.UpdateAsync(basket), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace Microsoft.eShopWeb.UnitTests
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenId()
|
||||
public void MatchesBasketWithGivenBasketId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||
|
||||
@@ -23,14 +23,37 @@ namespace Microsoft.eShopWeb.UnitTests
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_testBasketId, result.Id);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfIdNotPresent()
|
||||
public void MatchesNoBasketsIfBasketIdNotPresent()
|
||||
{
|
||||
int badId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badId);
|
||||
int badBasketId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||
|
||||
Assert.False(GetTestBasketCollection()
|
||||
.AsQueryable()
|
||||
.Any(spec.Criterias.FirstOrDefault()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBuyerId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||
|
||||
var result = GetTestBasketCollection()
|
||||
.AsQueryable()
|
||||
.FirstOrDefault(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_buyerId, result.BuyerId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesNoBasketsIfBuyerIdNotPresent()
|
||||
{
|
||||
string badBuyerId = "badBuyerId";
|
||||
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||
|
||||
Assert.False(GetTestBasketCollection()
|
||||
.AsQueryable()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
{
|
||||
public class CatalogFilterPaginatedSpecification
|
||||
{
|
||||
[Fact]
|
||||
public void ReturnsAllCatalogItems()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, null, null);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(4, result.ToList().Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Returns2CatalogItemsWithSameBrandAndTypeId()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterPaginatedSpecification(0, 10, 1, 1);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItemList = new List<CatalogItem>();
|
||||
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1", "Item 1", 1.00m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(1, 1, "Item 1.5", "Item 1.5", 1.50m, "TestUri1"));
|
||||
catalogItemList.Add(new CatalogItem(2, 2, "Item 2", "Item 2", 2.00m, "TestUri2"));
|
||||
catalogItemList.Add(new CatalogItem(3, 3, "Item 3", "Item 3", 3.00m, "TestUri3"));
|
||||
|
||||
return catalogItemList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests
|
||||
{
|
||||
public class CatalogFilterSpecificationFilter
|
||||
public class CatalogFilterSpecification
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(null, null, 5)]
|
||||
@@ -18,7 +17,7 @@ namespace Microsoft.eShopWeb.UnitTests
|
||||
[InlineData(2, 3, 0)]
|
||||
public void MatchesExpectedNumberOfItems(int? brandId, int? typeId, int expectedCount)
|
||||
{
|
||||
var spec = new CatalogFilterSpecification(brandId, typeId);
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogFilterSpecification(brandId, typeId);
|
||||
|
||||
var result = GetTestItemCollection()
|
||||
.AsQueryable()
|
||||
@@ -0,0 +1,55 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
{
|
||||
public class CatalogItemsSpecification
|
||||
{
|
||||
[Fact]
|
||||
public void MatchesSpecificCatalogItem()
|
||||
{
|
||||
var catalogItemIds = new int[] { 1 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.ToList());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesAllCatalogItems()
|
||||
{
|
||||
var catalogItemIds = new int[] { 1, 3 };
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CatalogItemsSpecification(catalogItemIds);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.ToList().Count);
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetTestCollection()
|
||||
{
|
||||
var catalogItems = new List<CatalogItem>();
|
||||
|
||||
var mockCatalogItem1 = new Mock<CatalogItem>(1, 1, "Item 1 description", "Item 1", 1.5m, "Item1Uri");
|
||||
mockCatalogItem1.SetupGet(x => x.Id).Returns(1);
|
||||
|
||||
var mockCatalogItem3 = new Mock<CatalogItem>(3, 3, "Item 3 description", "Item 3", 3.5m, "Item3Uri");
|
||||
mockCatalogItem3.SetupGet(x => x.Id).Returns(3);
|
||||
|
||||
catalogItems.Add(mockCatalogItem1.Object);
|
||||
catalogItems.Add(mockCatalogItem3.Object);
|
||||
|
||||
return catalogItems;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
{
|
||||
public class CustomerOrdersWithItemsSpecification
|
||||
{
|
||||
private readonly string _buyerId = "TestBuyerId";
|
||||
private Address _shipToAddress = new Address("Street", "City", "OH", "US", "11111");
|
||||
|
||||
[Fact]
|
||||
public void ReturnsOrderWithOrderedItem()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.FirstOrDefault(spec.Criterias.FirstOrDefault());
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.OrderItems);
|
||||
Assert.Equal(1, result.OrderItems.Count);
|
||||
Assert.NotNull(result.OrderItems.FirstOrDefault().ItemOrdered);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnsAllOrderWithAllOrderedItem()
|
||||
{
|
||||
var spec = new eShopWeb.ApplicationCore.Specifications.CustomerOrdersWithItemsSpecification(_buyerId);
|
||||
|
||||
var result = GetTestCollection()
|
||||
.AsQueryable()
|
||||
.Where(spec.Criterias.FirstOrDefault())
|
||||
.ToList();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(1, result[0].OrderItems.Count);
|
||||
Assert.NotNull(result[0].OrderItems.FirstOrDefault().ItemOrdered);
|
||||
Assert.Equal(2, result[1].OrderItems.Count);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[0].ItemOrdered);
|
||||
Assert.NotNull(result[1].OrderItems.ToList()[1].ItemOrdered);
|
||||
}
|
||||
|
||||
public List<Order> GetTestCollection()
|
||||
{
|
||||
var ordersList = new List<Order>();
|
||||
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
new OrderItem(new CatalogItemOrdered(1, "Product1", "testurl"), 10.50m, 1)
|
||||
}));
|
||||
ordersList.Add(new Order(_buyerId, _shipToAddress,
|
||||
new List<OrderItem>
|
||||
{
|
||||
new OrderItem(new CatalogItemOrdered(2, "Product2", "testurl"), 15.50m, 2),
|
||||
new OrderItem(new CatalogItemOrdered(2, "Product3", "testurl"), 20.50m, 1)
|
||||
}));
|
||||
|
||||
return ordersList;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user