Skip to content

Commit

Permalink
Add tests for wallet withdrawal: ensure exception is thrown for insuf…
Browse files Browse the repository at this point in the history
…ficient funds and balance is decreased on valid withdrawal
  • Loading branch information
Edrisym committed Nov 14, 2024
1 parent c6e362d commit 1454310
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 0 additions & 7 deletions E_Wallet/Models/Wallet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace E_Wallet.Models;

public class Wallet
Expand Down Expand Up @@ -40,20 +39,14 @@ public void Deposit(decimal amount)
{
// TODO -- ***is it ok to check on zero values**
if (decimal.IsNegative(amount) || amount == decimal.Zero)
{
NegativeBalanceException.Throw(amount);
}

Balance += amount;
}

public void Withdraw(decimal amount)
{
if (Balance < amount)
{
InsufficientFundsException.Throw(amount);
}

Balance -= amount;
}

Expand Down
22 changes: 22 additions & 0 deletions Wallet_Tests/Wallet_Wallet_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void Should_Throw_Exception_If_InitialBalance_Is_Under_Limit(string code,
[InlineData(-10)]
[InlineData(-200.9)]
[InlineData(0)]
[InlineData(null!)]

Check warning on line 46 in Wallet_Tests/Wallet_Wallet_Tests.cs

View workflow job for this annotation

GitHub Actions / build

Null should not be used for value type parameter 'amount' of type 'decimal'. Use a non-null value, or convert the parameter to a nullable type. (https://xunit.net/xunit.analyzers/rules/xUnit1012)

Check warning on line 46 in Wallet_Tests/Wallet_Wallet_Tests.cs

View workflow job for this annotation

GitHub Actions / build

Null should not be used for value type parameter 'amount' of type 'decimal'. Use a non-null value, or convert the parameter to a nullable type. (https://xunit.net/xunit.analyzers/rules/xUnit1012)
public void Should_Throw_Exception_When_Deposit_Amount_Is_Negative_Or_Zero(decimal amount)
{
var currency = Currency.Create("USD", "United States Dollar", 1.99m);
Expand All @@ -61,4 +62,25 @@ public void Should_Increase_Balance_When_Deposit_Amount_Is_Positive()
wallet.Deposit(amount: 100);
wallet.Balance.Should().Be(200);
}


[Fact]
public void Should_Throw_Exception_When_Balance_Is_Lower_Than_Withdrawal_Amount()
{
var currency = Currency.Create("USD", "United States Dollar", 1.99m);
var wallet = Wallet.Create(balance: 100, currency);
wallet.Withdraw(amount: 100);
wallet.Balance.Should().Be(0).And.BePositive();
;
}


[Fact]
public void Should_Decrease_Balance_When_Withdrawal()
{
var currency = Currency.Create("USD", "United States Dollar", 1.99m);
var wallet = Wallet.Create(balance: 100, currency);
var withdrawal = () => wallet.Withdraw(amount: 101);
withdrawal.Should().ThrowExactly<InsufficientFundsException>();
}
}

0 comments on commit 1454310

Please sign in to comment.