C# NUnit 即使使用 TestFixture 也找不到合适的构造函数
C# NUnit No suitable constructor found even when using TestFixture
我在 C# 中使用 NUnit 进行一些单元测试。我有这个 class 继承结构:
[TestFixture(null)]
public abstract class BaseTests
{
protected BaseTests(StatusesEnum? status)
{
}
[Test]
public abstract void TestMethod_1();
}
[TestFixture(null)]
public class SalesTests : BaseTests
{
protected SalesTests(StatusesEnum? status) : base(status)
{
}
//When I run this test from this class it throws the "No suitable constructor was found"
[Test]
public override void TestMethod_1()
{
}
}
//When I run the test from this class it works perfectly since it passes value to the constructor
public class CustomerTests : SalesTests
{
public CustomerTests() : base (StatusesEnum.New) { }
}
当我运行宁 CustomerTests
他们 运行 完美,他们按预期从 SalesTests
调用 TestMEthos_1
。
但是当我 运行 仅 SalesTests
class 时,我一直收到 No suitable constructor found
的异常。预期结果应该是 status
参数将为 null 并且测试应该通过,因为我正在测试方法中检查该参数。
我发现很多答案都说只需添加 [TestFixture]
属性,但这也没有帮助。
所以任何关于如何解决这个问题的想法都会很棒。
NUnit
似乎要求构造函数为 public。下一个设置对我有用(更改 SalesTests
ctor 可访问性修饰符并将 TestFixtureAttribute
添加到 CustomerTests
):
[TestFixture(null)]
public class SalesTests : BaseHfsTests
{
public SalesTests(StatusesEnum? status) : base(status)
{
}
[Test]
public override void TestMethod_1()
{
}
}
[TestFixture]
public class CustomerTests : SalesTests
{
public CustomerTests() : base(StatusesEnum.New)
{
}
}
我通过简单地将两个基数 类(BaseTests
和 SalesTests
)更改为 abstract
来解决它,因为 abstract class
不会 运行 自行测试,仅继承 类 运行 他们的测试。
public abstract class BaseTests
{
protected BaseTests(StatusesEnum? status)
{
}
[Test]
public abstract void TestMethod_1();
}
public abstract class SalesTests : BaseTests
{
protected SalesTests(StatusesEnum? status) : base(status)
{
}
[Test]
public override void TestMethod_1()
{
}
}
public class CustomerTests : SalesTests
{
public CustomerTests() : base (StatusesEnum.New) { }
}
我在 C# 中使用 NUnit 进行一些单元测试。我有这个 class 继承结构:
[TestFixture(null)]
public abstract class BaseTests
{
protected BaseTests(StatusesEnum? status)
{
}
[Test]
public abstract void TestMethod_1();
}
[TestFixture(null)]
public class SalesTests : BaseTests
{
protected SalesTests(StatusesEnum? status) : base(status)
{
}
//When I run this test from this class it throws the "No suitable constructor was found"
[Test]
public override void TestMethod_1()
{
}
}
//When I run the test from this class it works perfectly since it passes value to the constructor
public class CustomerTests : SalesTests
{
public CustomerTests() : base (StatusesEnum.New) { }
}
当我运行宁 CustomerTests
他们 运行 完美,他们按预期从 SalesTests
调用 TestMEthos_1
。
但是当我 运行 仅 SalesTests
class 时,我一直收到 No suitable constructor found
的异常。预期结果应该是 status
参数将为 null 并且测试应该通过,因为我正在测试方法中检查该参数。
我发现很多答案都说只需添加 [TestFixture]
属性,但这也没有帮助。
所以任何关于如何解决这个问题的想法都会很棒。
NUnit
似乎要求构造函数为 public。下一个设置对我有用(更改 SalesTests
ctor 可访问性修饰符并将 TestFixtureAttribute
添加到 CustomerTests
):
[TestFixture(null)]
public class SalesTests : BaseHfsTests
{
public SalesTests(StatusesEnum? status) : base(status)
{
}
[Test]
public override void TestMethod_1()
{
}
}
[TestFixture]
public class CustomerTests : SalesTests
{
public CustomerTests() : base(StatusesEnum.New)
{
}
}
我通过简单地将两个基数 类(BaseTests
和 SalesTests
)更改为 abstract
来解决它,因为 abstract class
不会 运行 自行测试,仅继承 类 运行 他们的测试。
public abstract class BaseTests
{
protected BaseTests(StatusesEnum? status)
{
}
[Test]
public abstract void TestMethod_1();
}
public abstract class SalesTests : BaseTests
{
protected SalesTests(StatusesEnum? status) : base(status)
{
}
[Test]
public override void TestMethod_1()
{
}
}
public class CustomerTests : SalesTests
{
public CustomerTests() : base (StatusesEnum.New) { }
}