forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding guards and more tests (dotnet-architecture#68)
* Adding single entity by spec method to repository * Adding guards and more unit tests
- Loading branch information
Showing
10 changed files
with
119 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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
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
40 changes: 40 additions & 0 deletions
40
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/SetQuantities.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 |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.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 |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |