【发布时间】:2012-01-05 17:21:26
【问题描述】:
我正在实验室进行单元测试,下面是我正在测试的应用程序的一段代码。大多数单元测试已经完成,但是关于下面的构造函数,我只是不知道如何测试它。例如,构造函数究竟对数组元素做了什么?什么是测试构造函数的好方法?
是否有善良的灵魂可以让我朝正确的方向前进?
public struct Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
...
public Triangle(Point[] s) {
sides = new double[s.Length];
sides[0] = Math.Sqrt(Math.Pow((double)(s[1].x - s[0].x), 2.0) + Math.Pow((double)(s[1].y - s[0].y), 2.0));
sides[1] = Math.Sqrt(Math.Pow((double)(s[1].x - s[2].x), 2.0) + Math.Pow((double)(s[1].x - s[2].x), 2.0));
sides[2] = Math.Sqrt(Math.Pow((double)(s[2].x - s[0].x), 2.0) + Math.Pow((double)(s[2].x - s[0].x), 2.0));
}
...
[TestMethod()]
public void TriangleConstructorTest1()
{
Point[] s = null; // TODO: Initialize to an appropriate value
Triangle target = new Triangle(s);
Assert.Inconclusive("TODO: Implement code to verify target");
}
【问题讨论】:
-
你确定他们要求你也测试构造函数吗?对我来说似乎毫无意义,因为你只是在做一些任务。
-
sides是否以任何方式暴露?Triangle可以使用的公共属性是什么? -
@Tudor:实际上我倾向于不同意。因为它正在做分配,所以它可能应该进行单元测试以确保它分配了预期的内容。但这只是我的 0.02 美元,每个人对待单元测试的方式都不同。
-
+1 给您的老师和/或教育机构,用于教学单元测试。 :)
-
另外,拥有公共属性比拥有公共字段更好。 IE。
public Point[] Sides { get; set; }而不是public Point[] sides;
标签: c# unit-testing mstest