nUnit中的ExpectedException给了我一个错误

时间:2023-01-16 15:41:04

I'm new to using Testing Tools on the .NET Framework, so I downloaded it from NuGet with help from ReSharper.

我不熟悉.NET Framework上的测试工具,所以我在ReSharper的帮助下从NuGet下载了它。

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:

我正在使用此快速入门来学习如何使用nUnit。我刚刚复制了代码,这个属性出现错误:

[ExpectedException(typeof(InsufficientFundsException))] //it is user defined Exception 

The error is:

错误是:

The type or namespace name 'ExpectedException' could not be found (are you missing a using directive or an assembly reference?)

找不到类型或命名空间名称'ExpectedException'(您是否缺少using指令或程序集引用?)

Why? And if I need such functionality, what should I replace it with?

为什么?如果我需要这样的功能,我应该用它替换它?

3 个解决方案

#1


56  

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.

如果您正在使用NUnit 3.0,那么您的错误是因为ExpectedExceptionAttribute已被删除。您应该使用像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);
}

To change this to work under NUnit 3.0, change it to the following:

要将其更改为在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>());
}

#2


11  

Not sure if this changed recently but with NUnit 3.4.0 it provides Assert.Throws<T>.

不确定最近是否改变但是使用NUnit 3.4.0它提供了Assert.Throws

[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)); 
}

#3


5  

If you still want to use Attributes, consider this:

如果您仍想使用属性,请考虑以下事项:

[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));
}

#1


56  

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.

如果您正在使用NUnit 3.0,那么您的错误是因为ExpectedExceptionAttribute已被删除。您应该使用像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);
}

To change this to work under NUnit 3.0, change it to the following:

要将其更改为在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>());
}

#2


11  

Not sure if this changed recently but with NUnit 3.4.0 it provides Assert.Throws<T>.

不确定最近是否改变但是使用NUnit 3.4.0它提供了Assert.Throws

[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)); 
}

#3


5  

If you still want to use Attributes, consider this:

如果您仍想使用属性,请考虑以下事项:

[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));
}