c# - C# NUnit 报告奇怪的错误:“没有找到合适的构造函数”
问题描述
我正在使用 VS2019 并使用 .NET Core 模板创建了一个 NUnit 测试项目,然后我添加了以下代码:
using NUnit.Framework;
namespace xxx
{
class Immutable
{
public Immutable(string _cur, string _addr)
{
Currency = _cur;
Address = _addr;
}
public string Currency { get; }
public string Address { get; }
[Test]
public static void Test() // reports this line has problem?
{
var m = new Immutable("usd", "us");
string s = m.Currency;
Assert.AreEqual("usd", s);
}
}
}
构建好,但是当我运行它时,测试资源管理器会报告这个:
Test
Source: xxx.cs line 17
Duration: < 1 ms
Message:
OneTimeSetUp: No suitable constructor was found
我不太明白问题是什么,如何解决?
解决方案
如果您的测试类具有参数化构造函数,则需要带有参数的 TestFixture 属性来构造它。
尝试这个:
[TestFixuture("usd", "us")]
class Immutable
{
...
}
见文件
并强烈建议您单独一个测试类进行测试。
[TestFixture]
public class ImmutableTest
{
[Test]
public void Test()
{
var m = new Immutable("usd", "us");
string s = m.Currency;
Assert.AreEqual("usd", s);
}
}
推荐阅读
- reactjs - React Hooks setTimeout 和 clearTimeout
- php - 如何在php中设置选定下拉列表的值?
- python-3.x - 在函数中设置实例成员 - 这里发生了什么?
- sapui5 - SAPUI5 获取与绑定关联的控件
- c - 如何使用字符指针并在其中输入字符串?
- node.js - express-validator 将验证提取到单独的文件中
- javascript - 使用解构获取对象中的第一个数组元素
- c# - 为什么 ASP .NET MVC 5 中没有 Request.Content?
- c# - 如何通过铸造优化许多条件?
- azure - 使用 Azure 容器实例中托管的 Docker 进行 Application Insights 遥测的详细程度