public class Bank { public decimal Balance { get; private set; } public void…
- User
public class Bank { public decimal Balance { get; private set; }
public void AddMoney(decimal amount) { if (amount < 1) { throw new ArgumentException($"{nameof(amount)} must be positive and non zero"); } Balance += amount; } public void TakeMoney(decimal amount) { if (amount > Balance) { throw new ArgumentException("Not enough money for transfer"); } Balance -= amount; }} just write tests for this class framework for tests XUnit
- Assistant
Using XUnit, you can write tests for the Bank class like this:
using Xunit; public class BankTests { [Fact] public void AddMoney_ShouldIncreaseBalance() { // Arrange Bank bank = new Bank(); decimal initialBalance = bank.Balance; decimal amount = 100; // Act bank.AddMoney(amount); // Assert Assert.Equal(initialBalance + amount, bank.Balance); } [Fact] public void AddMoney_ShouldThrowException_WhenAmountIsNegativeOrZero() { // Arrange Bank bank = new Bank(); decimal amount = -100; // Act & Assert Assert.Throws<ArgumentException>(() => bank.AddMoney(amount)); } [Fact] public void TakeMoney_ShouldDecreaseBalance() { // Arrange Bank bank = new Bank(); bank.AddMoney(500); decimal initialBalance = bank.Balance; decimal amount = 200; // Act bank.TakeMoney(amount); // Assert Assert.Equal(initialBalance - amount, bank.Balance); } [Fact] public void TakeMoney_ShouldThrowException_WhenAmountIsGreaterThanBalance() { // Arrange Bank bank = new Bank(); bank.AddMoney(500); decimal amount = 1000; // Act & Assert Assert.Throws<ArgumentException>(() => bank.TakeMoney(amount)); } } - User
public class BlackJackCasino { private const decimal START_CASINO_BALANCE = 10_000_000; private const long MIN_PLAYER_BALANCE = 10_000; private const long MAX_PLAYER_BALANCE = 1_000_000; private readonly CasinoBankBuilder _casinoBankBuilder; private readonly PlayerBankBuilder _playerBankBuilder; private readonly Bank _casinoBank; private readonly Croupier _croupier; public List Players { get; } = new();
- Assistant
- User
public class PlayerBankBuilder : IBankBuilder { private readonly long _minInitBalance; private readonly long _maxInitBalance;
private Bank _currentInstanceOfBank; private readonly Random _rnd; public PlayerBankBuilder(long minInitBalance, long maxInitBalance) { if (minInitBalance < 1) throw new ArgumentException($"{nameof(minInitBalance)} must be positive and not 0"); if (maxInitBalance < 1) throw new ArgumentException($"{nameof(maxInitBalance)} must be positive and not 0"); if (minInitBalance > maxInitBalance) throw new ArgumentException($"{nameof(minInitBalance)} must be lower or equal {nameof(maxInitBalance)}"); _minInitBalance = minInitBalance; _maxInitBalance = maxInitBalance; _currentInstanceOfBank = new Bank(); _rnd = new Random(); } public void BuildBalance() { _currentInstanceOfBank.AddMoney(_rnd.NextInt64(_minInitBalance, _maxInitBalance)); } public Bank GetBank() { return _currentInstanceOfBank; } public void ResetInstance() { _currentInstanceOfBank = new Bank(); }} write xunit tests
- Assistant