From 1debc5d6000bdaf3610dfae6f59d97a1209742e8 Mon Sep 17 00:00:00 2001 From: Oli Sturm Date: Wed, 22 Apr 2026 15:31:07 +0100 Subject: [PATCH] fix tests for fp2 --- csharp-fp2.Tests/AccountTests.cs | 37 +++++++----- csharp-fp2.Tests/WithdrawMoneyHandlerTests.cs | 59 +++++++++---------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/csharp-fp2.Tests/AccountTests.cs b/csharp-fp2.Tests/AccountTests.cs index bff9e50..302d5c4 100644 --- a/csharp-fp2.Tests/AccountTests.cs +++ b/csharp-fp2.Tests/AccountTests.cs @@ -10,42 +10,51 @@ public class AccountTests public void Opening_an_account_with_a_negative_balance_throws() { Assert.Throws(() => - new Account(new AccountId(Guid.NewGuid()), new Money(-1m)) + AccountDomain.Open(Guid.NewGuid(), new Money(-1m)) ); } [Fact] - public void Withdrawing_a_zero_amount_throws() + public void Withdrawing_a_zero_amount_returns_AmountMustBePositive() { - var account = new Account(new AccountId(Guid.NewGuid()), new Money(100m)); + var account = AccountDomain.Open(Guid.NewGuid(), new Money(100m)); - Assert.Throws(() => account.Withdraw(new Money(0m))); + var result = AccountDomain.Withdraw(account, new Money(0m)); + + Assert.IsType(result); } [Fact] - public void Withdrawing_a_negative_amount_throws() + public void Withdrawing_a_negative_amount_returns_AmountMustBePositive() { - var account = new Account(new AccountId(Guid.NewGuid()), new Money(100m)); + var account = AccountDomain.Open(Guid.NewGuid(), new Money(100m)); - Assert.Throws(() => account.Withdraw(new Money(-10m))); + var result = AccountDomain.Withdraw(account, new Money(-10m)); + + Assert.IsType(result); } [Fact] - public void Withdrawing_more_than_the_balance_throws() + public void Withdrawing_more_than_the_balance_returns_InsufficientBalance() { - var account = new Account(new AccountId(Guid.NewGuid()), new Money(100m)); + var account = AccountDomain.Open(Guid.NewGuid(), new Money(100m)); - Assert.Throws(() => account.Withdraw(new Money(101m))); + var result = AccountDomain.Withdraw(account, new Money(101m)); + + Assert.IsType(result); } [Fact] public void Successive_withdrawals_are_each_applied_to_the_running_balance() { - var account = new Account(new AccountId(Guid.NewGuid()), new Money(300m)); + var account = AccountDomain.Open(Guid.NewGuid(), new Money(300m)); - account.Withdraw(new Money(100m)); - account.Withdraw(new Money(100m)); + var result1 = AccountDomain.Withdraw(account, new Money(100m)); + var success1 = Assert.IsType(result1); - Assert.Equal(100m, account.Balance.Amount); + var result2 = AccountDomain.Withdraw(success1.Account, new Money(100m)); + var success2 = Assert.IsType(result2); + + Assert.Equal(100m, success2.Account.Balance.Amount); } } diff --git a/csharp-fp2.Tests/WithdrawMoneyHandlerTests.cs b/csharp-fp2.Tests/WithdrawMoneyHandlerTests.cs index 28e6ff4..61637b9 100644 --- a/csharp-fp2.Tests/WithdrawMoneyHandlerTests.cs +++ b/csharp-fp2.Tests/WithdrawMoneyHandlerTests.cs @@ -1,5 +1,4 @@ -using CsharpFp2.Applications; -using CsharpFp2.Contracts; +using CsharpFp2.Application; using CsharpFp2.Domain; using CsharpFp2.Infrastructure; @@ -10,76 +9,72 @@ namespace CsharpFp2.Tests; public class WithdrawMoneyHandlerTests { private static ( - WithdrawMoneyHandler handler, - InMemoryAccountRepository repository + WithdrawMoney withdraw, + LoadAccount loadAccount, + SaveAccount saveAccount ) BuildHandler() { - var repository = new InMemoryAccountRepository(); - var handler = new WithdrawMoneyHandler(repository); - return (handler, repository); + var (loadAccount, saveAccount) = InMemoryAccount.Create(); + var withdraw = AccountApplication.CreateWithdrawMoney(loadAccount, saveAccount); + return (withdraw, loadAccount, saveAccount); } [Fact] public void Withdrawing_from_an_account_reduces_its_balance_by_the_withdrawn_amount() { - var (handler, repository) = BuildHandler(); + var (withdraw, loadAccount, saveAccount) = BuildHandler(); var accountId = Guid.NewGuid(); - repository.Save(new Account(new AccountId(accountId), new Money(200m))); + saveAccount(AccountDomain.Open(accountId, new Money(200m))); - handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 75m }); + withdraw(accountId, 75m); - var account = repository.GetById(new AccountId(accountId))!; + var account = loadAccount(accountId)!; Assert.Equal(125m, account.Balance.Amount); } [Fact] public void Withdrawing_the_entire_balance_leaves_the_account_at_zero() { - var (handler, repository) = BuildHandler(); + var (withdraw, loadAccount, saveAccount) = BuildHandler(); var accountId = Guid.NewGuid(); - repository.Save(new Account(new AccountId(accountId), new Money(100m))); + saveAccount(AccountDomain.Open(accountId, new Money(100m))); - handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 100m }); + withdraw(accountId, 100m); - var account = repository.GetById(new AccountId(accountId))!; + var account = loadAccount(accountId)!; Assert.Equal(0m, account.Balance.Amount); } [Fact] public void Withdrawing_from_a_non_existent_account_throws() { - var (handler, _) = BuildHandler(); - var command = new WithdrawMoneyCommand { AccountId = Guid.NewGuid(), Amount = 50m }; + var (withdraw, _, _) = BuildHandler(); - Assert.Throws(() => handler.Handle(command)); + Assert.Throws(() => withdraw(Guid.NewGuid(), 50m)); } [Fact] - public void Withdrawing_more_than_the_available_balance_throws() + public void Withdrawing_more_than_the_available_balance_returns_InsufficientBalance() { - var (handler, repository) = BuildHandler(); + var (withdraw, _, saveAccount) = BuildHandler(); var accountId = Guid.NewGuid(); - repository.Save(new Account(new AccountId(accountId), new Money(50m))); + saveAccount(AccountDomain.Open(accountId, new Money(50m))); - Assert.Throws(() => - handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 100m }) - ); + var result = withdraw(accountId, 100m); + + Assert.IsType(result); } [Fact] public void After_a_failed_withdrawal_the_balance_is_unchanged() { - var (handler, repository) = BuildHandler(); + var (withdraw, loadAccount, saveAccount) = BuildHandler(); var accountId = Guid.NewGuid(); - repository.Save(new Account(new AccountId(accountId), new Money(50m))); + saveAccount(AccountDomain.Open(accountId, new Money(50m))); - try - { - handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 999m }); - } - catch (InsufficientBalanceException) { } + withdraw(accountId, 999m); - var account = repository.GetById(new AccountId(accountId))!; + var account = loadAccount(accountId)!; Assert.Equal(50m, account.Balance.Amount); } }