nUnit 中的 ExpectedException 给了我一个错误
ExpectedException in nUnit gave me an error
我不熟悉在 .NET Framework 上使用测试工具,所以我在 ReSharper 的帮助下从 NuGet 下载了它。
我正在使用这个 Quick Start 来学习如何使用 nUnit。我刚刚复制了代码,但这个属性出现了错误:
[ExpectedException(typeof(InsufficientFundsException))] //it is user defined Exception
错误是:
The type or namespace name 'ExpectedException' could not be found
(are you missing a using directive or an assembly reference?)
为什么?如果我需要这样的功能,我应该用什么来代替它?
如果您使用的是 NUnit 3.0,那么您的错误是因为 ExpectedExceptionAttribute
has been removed. You should instead use a construct like the 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>());
}
如果您仍想使用属性,请考虑:
[TestCase(null, typeof(ArgumentNullException))]
[TestCase("this is invalid", typeof(ArgumentException))]
public void SomeMethod_With_Invalid_Argument(string arg, Type expectedException)
{
Assert.Throws(expectedException, () => SomeMethod(arg));
}
不确定这是否最近发生了变化,但是 NUnit 3.4.0 提供了 Assert.Throws<T>
。
[Test]
public void TransferWithInsufficientFunds() {
Account source = new Account();
source.Deposit(200m);
Account destination = new Account();
destination.Deposit(150m);
Assert.Throws<InsufficientFundsException>(() => source.TransferFunds(destination, 300m));
}
我不熟悉在 .NET Framework 上使用测试工具,所以我在 ReSharper 的帮助下从 NuGet 下载了它。
我正在使用这个 Quick Start 来学习如何使用 nUnit。我刚刚复制了代码,但这个属性出现了错误:
[ExpectedException(typeof(InsufficientFundsException))] //it is user defined Exception
错误是:
The type or namespace name 'ExpectedException' could not be found (are you missing a using directive or an assembly reference?)
为什么?如果我需要这样的功能,我应该用什么来代替它?
如果您使用的是 NUnit 3.0,那么您的错误是因为 ExpectedExceptionAttribute
has been removed. You should instead use a construct like the 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>());
}
如果您仍想使用属性,请考虑:
[TestCase(null, typeof(ArgumentNullException))]
[TestCase("this is invalid", typeof(ArgumentException))]
public void SomeMethod_With_Invalid_Argument(string arg, Type expectedException)
{
Assert.Throws(expectedException, () => SomeMethod(arg));
}
不确定这是否最近发生了变化,但是 NUnit 3.4.0 提供了 Assert.Throws<T>
。
[Test]
public void TransferWithInsufficientFunds() {
Account source = new Account();
source.Deposit(200m);
Account destination = new Account();
destination.Deposit(150m);
Assert.Throws<InsufficientFundsException>(() => source.TransferFunds(destination, 300m));
}