Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Updating test projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Srushti Pasari94 committed Sep 18, 2023
1 parent d22efcc commit ee14ef3
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 91 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
</PackageVersion>
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageVersion Include="MinimalApi.Endpoint" Version="1.3.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.16" />
<PackageVersion Include="System.Net.Http.Json" Version="7.0.1" />
<PackageVersion Include="System.Security.Claims" Version="4.3.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.2" />
Expand All @@ -59,7 +61,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.0.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
Expand Down
3 changes: 1 addition & 2 deletions tests/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" >
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Moq;
//using Moq;
using NSubstitute;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests;

public class DeleteBasket
{
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 ShouldInvokeBasketRepositoryDeleteAsyncOnce()
{
var basket = new Basket(_buyerId);
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>(), default))
.ReturnsAsync(basket);
var basketService = new BasketService(_mockBasketRepo.Object, _mockLogger.Object);
basket.AddItem(1, 1.1m, 1);
basket.AddItem(2, 1.1m, 1);
_mockBasketRepo.GetByIdAsync(Arg.Any<int>(), default)
.Returns(basket);
var basketService = new BasketService(_mockBasketRepo, _mockLogger);

await basketService.DeleteBasketAsync(It.IsAny<int>());
await basketService.DeleteBasketAsync(1);

_mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny<Basket>(), default), Times.Once);
await _mockBasketRepo.Received().DeleteAsync(Arg.Any<Basket>(), default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.

Check warning on line 40 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'result' in 'Results<Basket>.Results(Basket result)'.
.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]
Expand All @@ -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);
Expand All @@ -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);

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

Check warning on line 97 in tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Results<Basket> Results<Basket>.Then(Basket value)'.

_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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Moq;
using NSubstitute;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
Expand Down Expand Up @@ -58,18 +58,18 @@ public void MatchesNoBasketsIfBuyerIdNotPresent()

public List<Basket> GetTestBasketCollection()
{
var basket1Mock = new Mock<Basket>(_buyerId);
basket1Mock.SetupGet(s => s.Id).Returns(1);
var basket2Mock = new Mock<Basket>(_buyerId);
basket2Mock.SetupGet(s => s.Id).Returns(2);
var basket3Mock = new Mock<Basket>(_buyerId);
basket3Mock.SetupGet(s => s.Id).Returns(_testBasketId);
var basket1Mock = Substitute.For<Basket>(_buyerId);
basket1Mock.Id.Returns(1);
var basket2Mock = Substitute.For<Basket>(_buyerId);
basket2Mock.Id.Returns(2);
var basket3Mock = Substitute.For<Basket>(_buyerId);
basket3Mock.Id.Returns(_testBasketId);

return new List<Basket>()
{
basket1Mock.Object,
basket2Mock.Object,
basket3Mock.Object
basket1Mock,
basket2Mock,
basket3Mock
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Moq;
using NSubstitute;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications;
Expand Down Expand Up @@ -36,14 +36,14 @@ 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 mockCatalogItem1 = Substitute.For<CatalogItem>(1, 1, "Item 1 description", "Item 1", 1.5m, "Item1Uri");
mockCatalogItem1.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);
var mockCatalogItem3 = Substitute.For<CatalogItem>(3, 3, "Item 3 description", "Item 3", 3.5m, "Item3Uri");
mockCatalogItem3.Id.Returns(3);

catalogItems.Add(mockCatalogItem1.Object);
catalogItems.Add(mockCatalogItem3.Object);
catalogItems.Add(mockCatalogItem1);
catalogItems.Add(mockCatalogItem3);

return catalogItems;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/UnitTests/Builders/BasketBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Moq;
using NSubstitute;

namespace Microsoft.eShopWeb.UnitTests.Builders;

Expand All @@ -22,17 +22,17 @@ public Basket Build()

public Basket WithNoItems()
{
var basketMock = new Mock<Basket>(BasketBuyerId);
basketMock.SetupGet(s => s.Id).Returns(BasketId);
var basketMock = Substitute.For<Basket>(BasketBuyerId);
basketMock.Id.Returns(BasketId);

_basket = basketMock.Object;
_basket = basketMock;
return _basket;
}

public Basket WithOneBasketItem()
{
var basketMock = new Mock<Basket>(BasketBuyerId);
_basket = basketMock.Object;
var basketMock = Substitute.For<Basket>(BasketBuyerId);
_basket = basketMock;
_basket.AddItem(2, 3.40m, 4);
return _basket;
}
Expand Down
13 changes: 6 additions & 7 deletions tests/UnitTests/MediatorHandlers/OrdersTests/GetMyOrders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Web.Features.MyOrders;
using Moq;
using NSubstitute;
using Xunit;

namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests;

public class GetMyOrders
{
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
private readonly IReadRepository<Order> _mockOrderRepository = Substitute.For<IReadRepository<Order>>();

public GetMyOrders()
{
var item = new OrderItem(new CatalogItemOrdered(1, "ProductName", "URI"), 10.00m, 10);
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
var address = new Address("", "", "", "", "");
Order order = new Order("buyerId", address, new List<OrderItem> { item });

_mockOrderRepository = new Mock<IReadRepository<Order>>();
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(), default)).ReturnsAsync(new List<Order> { order });

_mockOrderRepository.ListAsync(Arg.Any<ISpecification<Order>>(), default).Returns(new List<Order> { order });
}

[Fact]
public async Task NotReturnNullIfOrdersArePresIent()
{
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");

var handler = new GetMyOrdersHandler(_mockOrderRepository.Object);
var handler = new GetMyOrdersHandler(_mockOrderRepository);

var result = await handler.Handle(request, CancellationToken.None);

Expand Down
Loading

0 comments on commit ee14ef3

Please sign in to comment.