C# 构造函数的单元测试

C# 构造函数的单元测试,c#,unit-testing,mstest,C#,Unit Testing,Mstest,我正在进行一个实验室的单元测试,下面是我正在测试的应用程序的一段代码。大多数单元测试都已经完成,但是关于下面的构造函数,我就是不知道如何测试它。例如,构造函数对数组元素到底做了什么?测试施工人员的好方法是什么 有没有一个善良的灵魂能给我一个正确的方向的脚踢 public struct Point { public int x, y; public Point(int a, int b) { x = a; y = b; } } 我假

我正在进行一个实验室的单元测试,下面是我正在测试的应用程序的一段代码。大多数单元测试都已经完成,但是关于下面的构造函数,我就是不知道如何测试它。例如,构造函数对数组元素到底做了什么?测试施工人员的好方法是什么

有没有一个善良的灵魂能给我一个正确的方向的脚踢

  public struct Point { 
    public int x, y;

    public Point(int a, int b) {
      x = a;
      y = b;
    }
  }


我假设您有一个
Sides
属性,您可以断言从该属性返回的值应该是什么

你可能还想重构一下你的代码,对我来说,这三行代码看起来几乎相同,但参数不同


另外,您粘贴的构造函数与示例测试中的构造函数不一样。

也许我遗漏了一些东西,但这样做不好吗

   [TestMethod()]
        public void PointConstructorTest1()
        {
            Point target = new Point(1.5, 2.0);
            Assert.AreEqual(1.5, target.x);
            Assert.AreEqual(2.0, target.y);
        }

虽然测试变量赋值没有多大意义,但基于最初问题中的一些不一致性,很难回答,但我将基于构造函数代码,而不是示例测试

首先,我要测试一个<3或>3点的数组,并抛出一个适当的异常(您应该检查并在构造函数中抛出该异常)

首先,如果点数组不好,我会让构造函数抛出:

    public Triangle(Point[] s)
    {
        // you could use more specific ArgumentNullException for first,
        // IllegalOperationException for second, etc, you get the point 
        // (no pun intended).
        if (s== null || s.Length != 3 || s.Any(x => x == null)) 
            throw new ArgumentException("s");
        ...
    }
然后,我将测试空值或不正确的长度

    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void TriangleConstructorWithNullPoints()
    {
        Point[] s = null; 
        Triangle target = new Triangle(s);
    }

[TestMethod]        
[ExpectedException(typeof(ArgumentException))]        
public void TriangleConstructorWithFourPoints()        
{
    Point[] s = new Point[4];
    Triangle target = new Triangle(s);        
}
然后我会测试常见的情况。以3/4/5三角形为例:


然后我会测试任何边条件,比如长度为3的数组,但数组中的一个点为空(检查是否存在适当的异常),或者任何两个点是否相同,依此类推。

您正在谈论Traingle构造函数,是吗

看起来它正在使用计算三角形边的长度。
因此,您应该测试此计算是否正确完成

此外,您可以检查构造函数是否检测到无效参数,例如:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Cannot_Create_Passing_Null()
{
   new Triangle(null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_With_Less_Than_Three_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(1, 1) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_More_Than_Three_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(1, 1), new Point(2, 2), new Point(3, 3) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_Two_Identical_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(0, 0), new Point(1, 1) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_Empty_Array()
{
   new Triangle(new Point[] { });
}

我想开始说,如果你遵循of,那么你就不想拥有可变结构。现在,如果您站在单元测试的立场上,您会得出结论,测试不可变对象没有意义,因为它们只能有一个状态。最好的情况是,您最多可以测试构造函数没有执行类似的操作。x=b

您确定他们要求您也测试构造函数吗?对我来说似乎毫无意义,因为你只做了一些作业。
side
是否以任何方式公开?三角形的公共属性是什么?@Tudor:事实上我不同意。因为它在做任务,所以可能应该对它进行单元测试,以确保它分配了预期的内容。但那只是我的0.02美元,每个人对待单元测试的方式都不同。+1给你的老师和/或教育机构,教单元测试。:)此外,拥有公共属性比拥有公共字段更好。即
公共点[]边{get;set;}
而不是
公共点[]边    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void TriangleConstructorWithNullPoints()
    {
        Point[] s = null; 
        Triangle target = new Triangle(s);
    }

[TestMethod]        
[ExpectedException(typeof(ArgumentException))]        
public void TriangleConstructorWithFourPoints()        
{
    Point[] s = new Point[4];
    Triangle target = new Triangle(s);        
}
[TestMethod]
public void TriangleConstructorWithFourPoints()
{
    Point[] s = new []
    {
        new Point { x = 0, y = 0 },
        new Point { x = 4, y = 0 },
        new Point { x = 4, y = 3 }
    };

    Triangle target = new Triangle(s);

    Assert.IsTrue(target.Sides.Contains(3.0));
    Assert.IsTrue(target.Sides.Contains(4.0));
    Assert.IsTrue(target.Sides.Contains(5.0));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Cannot_Create_Passing_Null()
{
   new Triangle(null);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_With_Less_Than_Three_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(1, 1) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_More_Than_Three_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(1, 1), new Point(2, 2), new Point(3, 3) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_Two_Identical_Points()
{
   new Triangle(new[] { new Point(0, 0), new Point(0, 0), new Point(1, 1) });
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Cannot_Create_With_Empty_Array()
{
   new Triangle(new Point[] { });
}