add tests
This commit is contained in:
@@ -27,8 +27,9 @@ public sealed class Account : AggregateRoot<AccountId>
|
||||
if (amount.Amount <= 0)
|
||||
throw new InvalidOperationException("Withdrawal amount must be positive.");
|
||||
|
||||
// Sometimes, validation is modelled with custom exceptions instead
|
||||
if (Balance.Amount - amount.Amount < 0)
|
||||
throw new InvalidOperationException("Balance cannot go below zero.");
|
||||
throw new InsufficientBalanceException(Balance, amount);
|
||||
|
||||
Balance = Balance.Subtract(amount);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace CsharpOop.Domain;
|
||||
|
||||
/// Custom domain exception thrown when a withdrawal would cause the balance to go below zero
|
||||
public sealed class InsufficientBalanceException : InvalidOperationException
|
||||
{
|
||||
public Money CurrentBalance { get; }
|
||||
public Money RequestedAmount { get; }
|
||||
|
||||
public InsufficientBalanceException(Money currentBalance, Money requestedAmount)
|
||||
: base($"Insufficient balance. Current: {currentBalance.Amount:0.00}, Requested: {requestedAmount.Amount:0.00}")
|
||||
{
|
||||
CurrentBalance = currentBalance;
|
||||
RequestedAmount = requestedAmount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user