Browse Source
* Adding single entity by spec method to repository * Adding guards and more unit testsmain
committed by
GitHub
10 changed files with 119 additions and 1 deletions
@ -0,0 +1,23 @@ |
|||
using System; |
|||
|
|||
namespace ApplicationCore.Exceptions |
|||
{ |
|||
public class BasketNotFoundException : Exception |
|||
{ |
|||
public BasketNotFoundException(int basketId) : base($"No basket found with id {basketId}") |
|||
{ |
|||
} |
|||
|
|||
protected BasketNotFoundException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) |
|||
{ |
|||
} |
|||
|
|||
public BasketNotFoundException(string message) : base(message) |
|||
{ |
|||
} |
|||
|
|||
public BasketNotFoundException(string message, Exception innerException) : base(message, innerException) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using ApplicationCore.Exceptions; |
|||
using Microsoft.eShopWeb.ApplicationCore.Entities; |
|||
|
|||
namespace Ardalis.GuardClauses |
|||
{ |
|||
public static class FooGuard |
|||
{ |
|||
public static void NullBasket(this IGuardClause guardClause, int basketId, Basket basket) |
|||
{ |
|||
if (basket == null) |
|||
throw new BasketNotFoundException(basketId); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using ApplicationCore.Exceptions; |
|||
using ApplicationCore.Interfaces; |
|||
using ApplicationCore.Services; |
|||
using Microsoft.eShopWeb.ApplicationCore.Entities; |
|||
using Moq; |
|||
using System; |
|||
using Xunit; |
|||
|
|||
namespace UnitTests.ApplicationCore.Services.BasketServiceTests |
|||
{ |
|||
public class SetQuantities |
|||
{ |
|||
private int _invalidId = -1; |
|||
private Mock<IAsyncRepository<Basket>> _mockBasketRepo; |
|||
|
|||
public SetQuantities() |
|||
{ |
|||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async void ThrowsGivenInvalidBasketId() |
|||
{ |
|||
var basketService = new BasketService(_mockBasketRepo.Object, null, null, null); |
|||
|
|||
await Assert.ThrowsAsync<BasketNotFoundException>(async () => |
|||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>())); |
|||
} |
|||
|
|||
[Fact] |
|||
public async void ThrowsGivenNullQuantities() |
|||
{ |
|||
var basketService = new BasketService(null, null, null, null); |
|||
|
|||
await Assert.ThrowsAsync<ArgumentNullException>(async () => |
|||
await basketService.SetQuantities(123, null)); |
|||
} |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using ApplicationCore.Services; |
|||
using System; |
|||
using Xunit; |
|||
|
|||
namespace UnitTests.ApplicationCore.Services.BasketServiceTests |
|||
{ |
|||
public class TransferBasket |
|||
{ |
|||
[Fact] |
|||
public async void ThrowsGivenNullAnonymousId() |
|||
{ |
|||
var basketService = new BasketService(null, null, null, null); |
|||
|
|||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve")); |
|||
} |
|||
|
|||
[Fact] |
|||
public async void ThrowsGivenNullUserId() |
|||
{ |
|||
var basketService = new BasketService(null, null, null, null); |
|||
|
|||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue