TransferWithInsufficentFunds : InsufficientFundsException was expected 讓我們再一次修復Account代碼,按如下方法修改TransferFunds:
public void TransferFunds(Account destination, float amount)
{
destination.Deposit(amount);
if(balance-amount throw new InsufficientFundsException(); Withdraw(amount); } 編譯并運行測試-綠色的狀態條。成功了!但是等等,看看我們剛才編寫的代碼,我們會發現銀行可能在每個沒有成功的轉帳操作失去一筆錢。讓我們編寫一個測試來證明我們的疑慮,增加如下測試方法: [Test] public void TransferWithInsufficientFundsAtomicity() { Account source = new Account(); source.Deposit(200.00F); Account destination = new Account(); destination.Deposit(150.00F); try { source.TransferFunds(destination, 300.00F); } catch(InsufficientFundsException expected)
文章來源于領測軟件測試網 http://www.kjueaiud.com/