This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/nuget/Microsoft.Extensions.Identi…
…ty.Core-7.0.11
- Loading branch information
Showing
11 changed files
with
125 additions
and
92 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
24 changes: 13 additions & 11 deletions
24
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 |
---|---|---|
@@ -1,44 +1,46 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using Microsoft.eShopWeb.ApplicationCore.Services; | ||
using Microsoft.eShopWeb.ApplicationCore.Specifications; | ||
using Moq; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests; | ||
|
||
public class AddItemToBasket | ||
{ | ||
private readonly string _buyerId = "Test buyerId"; | ||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new(); | ||
private readonly Mock<IAppLogger<BasketService>> _mockLogger = new(); | ||
private readonly IRepository<Basket> _mockBasketRepo = Substitute.For<IRepository<Basket>>(); | ||
private readonly IAppLogger<BasketService> _mockLogger = Substitute.For<IAppLogger<BasketService>>(); | ||
|
||
[Fact] | ||
public async Task InvokesBasketRepositoryGetBySpecAsyncOnce() | ||
{ | ||
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); | ||
basket.AddItem(1, 1.5m); | ||
|
||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(basket); | ||
|
||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
|
||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m); | ||
|
||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once); | ||
await _mockBasketRepo.Received().FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokesBasketRepositoryUpdateAsyncOnce() | ||
{ | ||
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); | ||
basket.AddItem(1, 1.1m, 1); | ||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(basket); | ||
|
||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
|
||
await basketService.AddItemToBasket(basket.BuyerId, 1, 1.50m); | ||
|
||
_mockBasketRepo.Verify(x => x.UpdateAsync(basket, default), Times.Once); | ||
await _mockBasketRepo.Received().UpdateAsync(basket, default); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using Microsoft.eShopWeb.ApplicationCore.Services; | ||
using Microsoft.eShopWeb.ApplicationCore.Specifications; | ||
using Moq; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests; | ||
|
@@ -15,20 +15,36 @@ public class TransferBasket | |
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id"; | ||
private readonly string _nonexistentUserBasketBuyerId = "[email protected]"; | ||
private readonly string _existentUserBasketBuyerId = "[email protected]"; | ||
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new(); | ||
private readonly Mock<IAppLogger<BasketService>> _mockLogger = new(); | ||
private readonly IRepository<Basket> _mockBasketRepo = Substitute.For<IRepository<Basket>>(); | ||
private readonly IAppLogger<BasketService> _mockLogger = Substitute.For<IAppLogger<BasketService>>(); | ||
|
||
[Fact] | ||
public class Results<T> | ||
{ | ||
private readonly Queue<Func<T>> values = new Queue<Func<T>>(); | ||
public Results(T result) { values.Enqueue(() => result); } | ||
public Results<T> Then(T value) { return Then(() => value); } | ||
public Results<T> Then(Func<T> value) | ||
{ | ||
values.Enqueue(value); | ||
return this; | ||
} | ||
public T Next() { return values.Dequeue()(); } | ||
} | ||
|
||
[Fact] | ||
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnceIfAnonymousBasketNotExists() | ||
{ | ||
var anonymousBasket = null as Basket; | ||
var userBasket = new Basket(_existentUserBasketBuyerId); | ||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)) | ||
.ReturnsAsync(anonymousBasket) | ||
.ReturnsAsync(userBasket); | ||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
var anonymousBasket = null as Basket; | ||
var userBasket = new Basket(_existentUserBasketBuyerId); | ||
|
||
var results = new Results<Basket?>(anonymousBasket) | ||
.Then(userBasket); | ||
|
||
|
||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(x => results.Next()); | ||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); | ||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once); | ||
await _mockBasketRepo.Received().FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default); | ||
} | ||
|
||
[Fact] | ||
|
@@ -40,12 +56,15 @@ public async Task TransferAnonymousBasketItemsWhilePreservingExistingUserBasketI | |
var userBasket = new Basket(_existentUserBasketBuyerId); | ||
userBasket.AddItem(1, 10, 4); | ||
userBasket.AddItem(2, 99, 3); | ||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)) | ||
.ReturnsAsync(anonymousBasket) | ||
.ReturnsAsync(userBasket); | ||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
|
||
var results = new Results<Basket>(anonymousBasket) | ||
.Then(userBasket); | ||
|
||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(x => results.Next()); | ||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); | ||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once); | ||
await _mockBasketRepo.Received().UpdateAsync(userBasket, default); | ||
|
||
Assert.Equal(3, userBasket.Items.Count); | ||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 1 && x.UnitPrice == 10 && x.Quantity == 5); | ||
Assert.Contains(userBasket.Items, x => x.CatalogItemId == 2 && x.UnitPrice == 99 && x.Quantity == 3); | ||
|
@@ -57,25 +76,29 @@ public async Task RemovesAnonymousBasketAfterUpdatingUserBasket() | |
{ | ||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId); | ||
var userBasket = new Basket(_existentUserBasketBuyerId); | ||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)) | ||
.ReturnsAsync(anonymousBasket) | ||
.ReturnsAsync(userBasket); | ||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
|
||
var results = new Results<Basket>(anonymousBasket) | ||
.Then(userBasket); | ||
|
||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(x => results.Next()); | ||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); | ||
_mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once); | ||
_mockBasketRepo.Verify(x => x.DeleteAsync(anonymousBasket, default), Times.Once); | ||
await _mockBasketRepo.Received().UpdateAsync(userBasket, default); | ||
await _mockBasketRepo.Received().DeleteAsync(anonymousBasket, default); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatesNewUserBasketIfNotExists() | ||
{ | ||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId); | ||
var userBasket = null as Basket; | ||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)) | ||
.ReturnsAsync(anonymousBasket) | ||
.ReturnsAsync(userBasket); | ||
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object); | ||
|
||
var results = new Results<Basket?>(anonymousBasket) | ||
.Then(userBasket); | ||
|
||
_mockBasketRepo.FirstOrDefaultAsync(Arg.Any<BasketWithItemsSpecification>(), default).Returns(x => results.Next()); | ||
var basketService = new BasketService(_mockBasketRepo, _mockLogger); | ||
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId); | ||
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once); | ||
await _mockBasketRepo.Received().AddAsync(Arg.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default); | ||
} | ||
} |
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
Oops, something went wrong.