如果你使用的是NUnit 3.0,那麼你的錯誤是因爲ExpectedExceptionAttribute
has been removed。你應該使用像Throws Constraint這樣的結構。
例如,你鏈接的教程有這個測試:
[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void TransferWithInsufficientFunds()
{
Account source = new Account();
source.Deposit(200m);
Account destination = new Account();
destination.Deposit(150m);
source.TransferFunds(destination, 300m);
}
要改變這種NUnit的3.0下工作,將其更改爲以下:
[Test]
public void TransferWithInsufficientFunds()
{
Account source = new Account();
source.Deposit(200m);
Account destination = new Account();
destination.Deposit(150m);
Assert.That(() => source.TransferFunds(destination, 300m),
Throws.TypeOf<InsufficientFundsException>());
}
顯示什麼錯誤?錯誤是在nUnit還是IDE中顯示的? – Chawin
我認爲你的代碼返回一個異常,它不是InsufficientFundsException –