创建一个类
namespace ClassLibrary1;
public class BankAccount
{
private double balance;
public BankAccount()
{
}
public BankAccount(double balance)
{
this.balance = balance;
}
public double Balance
{
get { return balance; }
}
public void Add(double amount)
{
if (amount < 0)
{
throw new ArgumentOutOfRangeException(nameof(amount));
}
balance += amount;
}
public void Withdraw(double amount)
{
if (amount > balance)
{
throw new ArgumentOutOfRangeException(nameof(amount));
}
if (amount < 0)
{
throw new ArgumentOutOfRangeException(nameof(amount));
}
balance -= amount;
}
public void TransferFundsTo(BankAccount? otherAccount, double amount)
{
if (otherAccount is null)
{
throw new ArgumentNullException(nameof(otherAccount));
}
Withdraw(amount);
otherAccount.Add(amount);
}
}
编写单元测试代码
using ClassLibrary1;
namespace TestProject1;
public class Tests
{
public class BankAccountTests
{
private BankAccount _account;
[SetUp]
public void Setup()
{
_account = new BankAccount(1000);
}
[Test]
public void Adding_Funds_Updates_Balance()
{
_account.Add(500);
Assert.That(_account.Balance, Is.EqualTo(1500));
}
[Test]
public void Adding_Negative_Funds_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() => _account.Add(-500));
}
[Test]
public void Withdrawing_Funds_Updates_Balance()
{
_account.Withdraw(500);
Assert.AreEqual(500, _account.Balance);
}
[Test]
public void Withdrawing_Negative_Funds_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() => _account.Withdraw(-500));
}
[Test]
public void Withdrawing_More_Than_Balance_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() => _account.Withdraw(2000));
}
[Test]
public void Transfering_Funds_Updates_Both_Accounts()
{
var otherAccount = new BankAccount();
_account.TransferFundsTo(otherAccount, 500);
Assert.That(_account.Balance, Is.EqualTo(500));
Assert.That(otherAccount.Balance, Is.EqualTo(500));
}
[Test]
public void Transfer_To_Non_Existing_Account_Throws()
{
Assert.Throws<ArgumentNullException>(() => _account.TransferFundsTo(null, 2000));
}
}
}