nUnit中的ExpectedException给了我一个错误 [英] ExpectedException in nUnit gave me an error
问题描述
我是不熟悉在.NET Framework上使用测试工具的人,因此我在ReSharper的帮助下从NuGet下载了它。
I'm new to using Testing Tools on the .NET Framework, so I downloaded it from NuGet with help from ReSharper.
我正在使用此快速入门以了解如何使用nUnit。我刚刚复制了代码,此属性出现错误:
I am using this Quick Start to learn how to use nUnit. I had just copied the code and an error came up on this attribute:
[ExpectedException(typeof(InsufficientFundsException))] //it is user defined Exception
错误是:
找不到类型或名称空间名称'ExpectedException'
(您是否缺少using指令或程序集引用?)
The type or namespace name 'ExpectedException' could not be found (are you missing a using directive or an assembly reference?)
为什么?如果我需要这样的功能,我应该用什么代替它?
Why? And if I need such functionality, what should I replace it with?
推荐答案
如果您使用的是NUnit 3.0,那么您的错误是因为 ExpectedExceptionAttribute
已被删除。相反,您应该使用 Throws Constraint 这样的构造。
If you're using NUnit 3.0, then your error is because the ExpectedExceptionAttribute
has been removed. You should instead use a construct like the Throws Constraint.
例如,您链接的教程进行了以下测试:
For example, the tutorial you linked has this test:
[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下工作,请将其更改为以下内容:
To change this to work under NUnit 3.0, change it to the following:
[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中的ExpectedException给了我一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!