Update Specification and other packages to latest version (#582)
* Updating repositories and specification version Need to fix broken tests * removing test that would just be testing mocked result now * Refactored from IAsyncRepository and removed it. Tests pass. * Update packages
This commit is contained in:
@@ -11,25 +11,20 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
public class AddItemToBasket
|
||||
{
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
|
||||
public AddItemToBasket()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
}
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnce()
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||
|
||||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -37,7 +32,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
{
|
||||
var basket = new Basket(_buyerId);
|
||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
|
||||
@@ -10,12 +10,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
public class DeleteBasket
|
||||
{
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
|
||||
public DeleteBasket()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
}
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
|
||||
|
||||
@@ -1,40 +1,35 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Moq;
|
||||
using System;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
{
|
||||
public class SetQuantities
|
||||
{
|
||||
private readonly int _invalidId = -1;
|
||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
|
||||
public SetQuantities()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenInvalidBasketId()
|
||||
{
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullQuantities()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await basketService.SetQuantities(123, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
{
|
||||
public class SetQuantities
|
||||
{
|
||||
private readonly int _invalidId = -1;
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenInvalidBasketId()
|
||||
{
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
|
||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullQuantities()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||
await basketService.SetQuantities(123, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,36 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Moq;
|
||||
using System;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
{
|
||||
public class TransferBasket
|
||||
{
|
||||
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
||||
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
||||
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
||||
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
||||
|
||||
public TransferBasket()
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||
{
|
||||
public class TransferBasket
|
||||
{
|
||||
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
||||
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
||||
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
||||
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
||||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullAnonymousId()
|
||||
{
|
||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullAnonymousId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullUserId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThrowsGivenNullUserId()
|
||||
{
|
||||
var basketService = new BasketService(null, null);
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -43,12 +38,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
{
|
||||
var anonymousBasket = null as Basket;
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -60,7 +55,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
userBasket.AddItem(1, 10, 4);
|
||||
userBasket.AddItem(2, 99, 3);
|
||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
@@ -77,7 +72,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
@@ -91,12 +86,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
||||
{
|
||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||
var userBasket = null as Basket;
|
||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||
.ReturnsAsync(anonymousBasket)
|
||||
.ReturnsAsync(userBasket);
|
||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
|
||||
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Ardalis.Specification.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
@@ -13,16 +12,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
private readonly int _testBasketId = 123;
|
||||
private readonly string _buyerId = "Test buyerId";
|
||||
|
||||
// tests with specifications can use an evaluator or just WhereExpressions.FirstOrDefault if only one
|
||||
private readonly SpecificationEvaluator<Basket> _evaluator = new SpecificationEvaluator<Basket>();
|
||||
|
||||
[Fact]
|
||||
public void MatchesBasketWithGivenBasketId()
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||
|
||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
||||
.FirstOrDefault();
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_testBasketId, result.Id);
|
||||
@@ -34,8 +29,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
int badBasketId = -1;
|
||||
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||
|
||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
||||
.Any();
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
@@ -45,8 +39,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
{
|
||||
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||
|
||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
||||
.FirstOrDefault();
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_buyerId, result.BuyerId);
|
||||
@@ -58,8 +51,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
||||
string badBuyerId = "badBuyerId";
|
||||
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||
|
||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
||||
.Any();
|
||||
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user