fix tests for fp2
This commit is contained in:
@@ -10,42 +10,51 @@ public class AccountTests
|
|||||||
public void Opening_an_account_with_a_negative_balance_throws()
|
public void Opening_an_account_with_a_negative_balance_throws()
|
||||||
{
|
{
|
||||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||||
new Account(new AccountId(Guid.NewGuid()), new Money(-1m))
|
AccountDomain.Open(Guid.NewGuid(), new Money(-1m))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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<InvalidOperationException>(() => account.Withdraw(new Money(0m)));
|
var result = AccountDomain.Withdraw(account, new Money(0m));
|
||||||
|
|
||||||
|
Assert.IsType<WithdrawResult.AmountMustBePositive>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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<InvalidOperationException>(() => account.Withdraw(new Money(-10m)));
|
var result = AccountDomain.Withdraw(account, new Money(-10m));
|
||||||
|
|
||||||
|
Assert.IsType<WithdrawResult.AmountMustBePositive>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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<InsufficientBalanceException>(() => account.Withdraw(new Money(101m)));
|
var result = AccountDomain.Withdraw(account, new Money(101m));
|
||||||
|
|
||||||
|
Assert.IsType<WithdrawResult.InsufficientBalance>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Successive_withdrawals_are_each_applied_to_the_running_balance()
|
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));
|
var result1 = AccountDomain.Withdraw(account, new Money(100m));
|
||||||
account.Withdraw(new Money(100m));
|
var success1 = Assert.IsType<WithdrawResult.Success>(result1);
|
||||||
|
|
||||||
Assert.Equal(100m, account.Balance.Amount);
|
var result2 = AccountDomain.Withdraw(success1.Account, new Money(100m));
|
||||||
|
var success2 = Assert.IsType<WithdrawResult.Success>(result2);
|
||||||
|
|
||||||
|
Assert.Equal(100m, success2.Account.Balance.Amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using CsharpFp2.Applications;
|
using CsharpFp2.Application;
|
||||||
using CsharpFp2.Contracts;
|
|
||||||
using CsharpFp2.Domain;
|
using CsharpFp2.Domain;
|
||||||
using CsharpFp2.Infrastructure;
|
using CsharpFp2.Infrastructure;
|
||||||
|
|
||||||
@@ -10,76 +9,72 @@ namespace CsharpFp2.Tests;
|
|||||||
public class WithdrawMoneyHandlerTests
|
public class WithdrawMoneyHandlerTests
|
||||||
{
|
{
|
||||||
private static (
|
private static (
|
||||||
WithdrawMoneyHandler handler,
|
WithdrawMoney withdraw,
|
||||||
InMemoryAccountRepository repository
|
LoadAccount loadAccount,
|
||||||
|
SaveAccount saveAccount
|
||||||
) BuildHandler()
|
) BuildHandler()
|
||||||
{
|
{
|
||||||
var repository = new InMemoryAccountRepository();
|
var (loadAccount, saveAccount) = InMemoryAccount.Create();
|
||||||
var handler = new WithdrawMoneyHandler(repository);
|
var withdraw = AccountApplication.CreateWithdrawMoney(loadAccount, saveAccount);
|
||||||
return (handler, repository);
|
return (withdraw, loadAccount, saveAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Withdrawing_from_an_account_reduces_its_balance_by_the_withdrawn_amount()
|
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();
|
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);
|
Assert.Equal(125m, account.Balance.Amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Withdrawing_the_entire_balance_leaves_the_account_at_zero()
|
public void Withdrawing_the_entire_balance_leaves_the_account_at_zero()
|
||||||
{
|
{
|
||||||
var (handler, repository) = BuildHandler();
|
var (withdraw, loadAccount, saveAccount) = BuildHandler();
|
||||||
var accountId = Guid.NewGuid();
|
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);
|
Assert.Equal(0m, account.Balance.Amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Withdrawing_from_a_non_existent_account_throws()
|
public void Withdrawing_from_a_non_existent_account_throws()
|
||||||
{
|
{
|
||||||
var (handler, _) = BuildHandler();
|
var (withdraw, _, _) = BuildHandler();
|
||||||
var command = new WithdrawMoneyCommand { AccountId = Guid.NewGuid(), Amount = 50m };
|
|
||||||
|
|
||||||
Assert.Throws<InvalidOperationException>(() => handler.Handle(command));
|
Assert.Throws<InvalidOperationException>(() => withdraw(Guid.NewGuid(), 50m));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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();
|
var accountId = Guid.NewGuid();
|
||||||
repository.Save(new Account(new AccountId(accountId), new Money(50m)));
|
saveAccount(AccountDomain.Open(accountId, new Money(50m)));
|
||||||
|
|
||||||
Assert.Throws<InsufficientBalanceException>(() =>
|
var result = withdraw(accountId, 100m);
|
||||||
handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 100m })
|
|
||||||
);
|
Assert.IsType<WithdrawResult.InsufficientBalance>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void After_a_failed_withdrawal_the_balance_is_unchanged()
|
public void After_a_failed_withdrawal_the_balance_is_unchanged()
|
||||||
{
|
{
|
||||||
var (handler, repository) = BuildHandler();
|
var (withdraw, loadAccount, saveAccount) = BuildHandler();
|
||||||
var accountId = Guid.NewGuid();
|
var accountId = Guid.NewGuid();
|
||||||
repository.Save(new Account(new AccountId(accountId), new Money(50m)));
|
saveAccount(AccountDomain.Open(accountId, new Money(50m)));
|
||||||
|
|
||||||
try
|
withdraw(accountId, 999m);
|
||||||
{
|
|
||||||
handler.Handle(new WithdrawMoneyCommand { AccountId = accountId, Amount = 999m });
|
|
||||||
}
|
|
||||||
catch (InsufficientBalanceException) { }
|
|
||||||
|
|
||||||
var account = repository.GetById(new AccountId(accountId))!;
|
var account = loadAccount(accountId)!;
|
||||||
Assert.Equal(50m, account.Balance.Amount);
|
Assert.Equal(50m, account.Balance.Amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user