forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding additional unit tests (dotnet-architecture#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
- Loading branch information
1 parent
d8848a9
commit a0ba412
Showing
13 changed files
with
271 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/UnitTests/ApplicationCore/Entities/BasketTests/BasketRemoveEmptyItems.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/UnitTests/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/UnitTests/ApplicationCore/Specifications/CatalogItemsSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/UnitTests/ApplicationCore/Specifications/CustomerOrdersWithItemsSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |