简体   繁体   English

为什么我的C#TestClass类(错误地)抱怨其父级没有无参数构造函数?

[英]Why does my C# TestClass class complain (wrongly) that its parent doesn't have a parameterless constructor?

I am doing some TDD in C# (Visual Studio 2012) using MSTest. 我正在使用MSTest在C#(Visual Studio 2012)中进行一些TDD。 I have a class declared with the [TestClass] attribute. 我有一个用[TestClass]属性声明的类。 My test class inherits from the class I am trying to test. 我的测试类继承自我要测试的类。

The parent class has a parameterless constructor, and yet I am getting a build error 父类具有无参数的构造函数,但是我遇到了构建错误

'RatingsClass.OutputLine' does not contain a constructor that takes 0 arguments

Here is my base class with its parameterless constructor: 这是我的基类及其无参数构造函数:

namespace RatingsClasses
{
    public class OutputLine
    {
        public OutputLine()
        {
            Initialise("Parameterless constructor called");
        }
(and so on)

Here is the test class which inherits from the base class. 这是从基类继承的测试类。 This causes the following build error: 这将导致以下生成错误:

'RatingsClass.OutputLine' does not contain a constructor that takes 0 arguments

Code: 码:

using RatingsClasses;

namespace RatingsKataV2
{
    [TestClass]
    public class RatingsTests: OutputLine
    {
        [TestMethod]
        public void TestSingleTripRatingIs20()
        {
(and so on)

Also when I try to call the base class's parameterless constructor directly from the derived class's constructor, I get lots of red squiggly lines and syntax errors such as Constructor must have body and Unexpected token . 同样,当我尝试直接从派生类的构造函数调用基类的无参数构造函数时,会出现很多红色的波浪线,并且语法错误(例如Constructor must have bodyUnexpected token

Here is my attempt to call the base constructor explicitly: 这是我尝试显式调用基本构造函数的尝试:

using RatingsClasses;

namespace RatingsKataV2
{
    [TestClass]
    public class RatingsTests: OutputLine
    {
        public RatingsTests(): OutputLine()
        {

        }
(the rest of the class goes here)

What am I doing wrong? 我究竟做错了什么?

The reason I am doing it this way is that I would like to test the various private members of my base class are being populated correctly. 我这样做的原因是,我想测试是否正确填充了基类的各个私有成员。 Rather than provide getters and setters or make those members public, it seemed to make sense that my test class simply inherit from the class it is testing, so that it can directly access those private members. 与其提供getter和setter或将这些成员公开,不如说我的测试类只是从正在测试的类继承而来,以便它可以直接访问那些私有成员,这似乎是有道理的。 Is there a better way of doing this? 有更好的方法吗?

Your syntax is wrong for calling the base constructor. 您的语法错误,无法调用基本构造函数。 You should use the base keyword: 您应该使用base关键字:

public RatingsTest()
    : base()
{

}

The TestClass attribute is for classes that contain unit tests, not for classes that are the targets of those tests. TestClass属性适用于包含单元测试的类,而不适用于作为那些测试目标的类。 By deriving from the target under test, you are also extending that target to RatingsTests. 通过从被测目标派生,您还可以将该目标扩展到RatingsTests。

RatingsTests should not need to inherit from OutputLine in order to test, but the constructor shoud read. RatingsTests不需要从OutputLine继承就可以进行测试,但是构造函数应该阅读。

public RatingsTests(): base() {}

You use base to explicitly choose the constructor of the the base class that should be used: 您可以使用base显式选择应使用的基类的构造函数:

public RatingsTests() : base()
{
}

However, this is unnecessary, because the parameterless constructor is the default one being used when this is omitted. 但是,这是不必要的,因为无参构造函数是默认的构造函数(省略该构造函数时)。

The first code you showed should work, so I guess that your actual code is different or you have a second class called OutputLine somewhere in your code base. 您显示的第一个代码应该可以工作,因此我想您的实际代码是不同的,或者您在代码库中的某个地方有一个名为OutputLine的第二类。

Make sure that you are using the correct OutputLine class and that this class actually has a public parameterless constructor. 确保使用正确的OutputLine类,并且该类实际上具有public参数构造函数。 If public is missing, it is automatically private and can't be used by a derived class. 如果缺少public ,则它将自动为private ,并且不能被派生类使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM