正在解析 NUnit "No suitable constructor was found" 消息

Resolving NUnit "No suitable constructor was found" message

我有一个单元测试class:

[TestFixture]
public class SomeClassIntegrationTests : SomeClass

使用public构造函数:

public SomeClassIntegrationTests (ILogger l) : base(l)
{
}

当我尝试 运行 测试时,出现 "No suitable constructor was found" 错误。

我尝试将 TestFixture 属性更改为 [TestFixture(typeof(ILogger))],但结果是相同的错误消息,不允许我 运行 或调试测试。

知道如何修改 TestFixture 属性以使测试达到 运行 或以其他方式解决此问题吗?

您可能需要一个 class 实现 ILogger 的实例。

选项 1:使用 null(如果确实不需要记录器):

[TestFixture(null)]

选项 2:始终使用相同的具体 class(或模拟):添加无参数构造函数

SomeClassIntegrationTests()
: this(new MyLogger())
{
}

[TestFixture]

选项 3:您可能希望使用不同的记录器进行测试

SomeClassIntegrationTests(Type t)
: this((Ilogger)Activator.CreateInstance(t))
{
}

[TestFixture(typeof(MyLogger))]