我正在学习为用C#编写的项目创建单元测试。我一直在MSDN网站上进行示例,现在我只是在数量小于零时如何创建单元测试。当我运行它时,单元测试应该失败。不过,我已经写在下面的方法,它通过:( 会有人请让我知道我需要做修复它感谢C中银行类的单元测试#
这是我到目前为止有:?
// unit test method
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Debit_WhenAmountIsLessThanZero_ShouldThrowArgumentOutOfRange()
{
// arrange
double beginningBalance = 11.99;
double debitAmount = -100.00;
BankAccount account = new BankAccount("Mr. Bryan Walton", beginningBalance);
// act
account.Debit(debitAmount);
// Not sure about this one
// on my main program, I use if...else to handle
// the situation when amount > balance by throwing the exception
double actual = account.Balance;
Assert.IsTrue(actual < 0, "Actual balance is greater than 0");
}
这是我在
public void Debit(double amount)
{
if (m_frozen)
{
throw new Exception("Account frozen");
}
if (amount > m_balance)
{
throw new ArgumentOutOfRangeException("amount");
}
if (amount < 0)
{
throw new ArgumentOutOfRangeException("amount");
}
m_balance -= amount;
}
您还应该添加BankAccount类的代码。 – EfrainReyes
如果BankAccount#Debit引发异常,Assert代码将永远不会到达,因此您的测试将会成功,因为您预计会发生异常 – Machinarius
'Assert.IsTrue(实际<0,“实际余额大于0”); '倒退了。您断言实际数量是**小于**零。 – Blorgbeard