Remove basket items when basket is deleted (#170)
* Proving out removing the basket items at the same time the basket is deleted * Fixing existing unit tests * Adding a unit test for the Deletebasket method * Rename test and deleting basket items before basket * Added integration test for DeleteAsync method for BasketItems - Also added a BasketBuilder to create a Basket with no items, or one item.
This commit is contained in:
committed by
Steve Smith
parent
95285593c9
commit
eb02750841
@@ -0,0 +1,37 @@
|
||||
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 DeleteBasket
|
||||
{
|
||||
private Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
private Mock<IAsyncRepository<BasketItem>> _mockBasketItemRepo;
|
||||
|
||||
public DeleteBasket()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
_mockBasketItemRepo = new Mock<IAsyncRepository<BasketItem>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_InvokeBasketRepoOnceAndBasketItemRepoTwice_Given_TwoItemsInBasket()
|
||||
{
|
||||
var basket = new Basket();
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
basket.AddItem(2, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny<int>()))
|
||||
.ReturnsAsync(basket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null, null, null, _mockBasketItemRepo.Object);
|
||||
|
||||
await basketService.DeleteBasketAsync(It.IsAny<int>());
|
||||
|
||||
_mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny<Basket>()), Times.Once);
|
||||
_mockBasketItemRepo.Verify(x => x.DeleteAsync(It.IsAny<BasketItem>()), Times.Exactly(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
[Fact]
|
||||
public async void ThrowsGivenInvalidBasketId()
|
||||
{
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null, null, null);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null, null, null, null);
|
||||
|
||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||
@@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
[Fact]
|
||||
public async void ThrowsGivenNullQuantities()
|
||||
{
|
||||
var basketService = new BasketService(null, null, null, null);
|
||||
var basketService = new BasketService(null, null, null, null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await basketService.SetQuantities(123, null));
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
[Fact]
|
||||
public async void ThrowsGivenNullAnonymousId()
|
||||
{
|
||||
var basketService = new BasketService(null, null, null, null);
|
||||
var basketService = new BasketService(null, null, null, null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
[Fact]
|
||||
public async void ThrowsGivenNullUserId()
|
||||
{
|
||||
var basketService = new BasketService(null, null, null, null);
|
||||
var basketService = new BasketService(null, null, null, null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user