![](/img/trans.png)
[英]C# NUnit No suitable constructor found even when using TestFixture
[英]C# NUnit reports weird error: "No suitable constructor was found"
我正在使用 VS2019 並使用 .net 核心模板創建了一個 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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.