用c#中的实际方法进行单元测试

时间:2013-01-27 12:45:55

标签: c# visual-studio-2010 unit-testing

您好我正在为我的项目进行单元测试,我想在一个名为Measurementool的抽象类中检查一个getmin方法。该方法从当前矩阵返回selectedarea矩阵的最小值。我的项目有一个矩形区域,实际上是N * N矩阵。

我需要矩阵内的最小值。我为调试编写单元测试并测试getmin值,但我没有看到实际值的期望值。 selectedarea矩阵填充零,未在测试方法中设置预期值。我认为,它考虑了两个矩阵。当新矩阵创建如下时,零值矩阵是错误的:

MatrixHolder selectedArea = 
           new MatrixHolder(new double[areaHeight * areaWidth], areaWidth);

单元测试代码:

[TestMethod()]
public void GetMinTest()
{
    AreaMeasurmentTool target = new Rectangle(); 
    double[,] MatrixResult = new double[,] {
    {10, 0, 20, 10, 10},
    {12, 2, 30, 15, 16},
    {19, 0, 43, 10, 17},
    {80, 0, 65, 18, 13},
    {70, 4, 10, 10, 10}};
    double []SelectedArea1 = new double[9]{2, 30, 15, 1, 43, 10, 2, 65, 18};
    MatrixHolder currentMatrix = new MatrixHolder(SelectedArea1, 3); 
    double expected = 1; 
    double actual;
    actual = target.GetMin(currentMatrix);
    Assert.AreEqual(expected, actual);
}

和类MeasurementTool:

public virtual double GetMin(MatrixHolder currentMatrix)
{
    if (currentMatrix == null)
    {
        throw new Exception("the image Matrix cannot be null.");
    }

    double minValue = 0;

    SetSelectArea(currentMatrix);
    minValue = CalculateMin();
    minValue = Math.Round(minValue, 2);

    return minValue;
}

private void SetSelectArea(MatrixHolder currentMatrix)
{
    PointHolder start = new PointHolder(0, 0);
    PointHolder end = new PointHolder(0, 0);

    SetBoundries(start, end);


    int areaHeight = (end.Y - start.Y);
    int areaWidth = (end.X - start.X);

    MatrixHolder selectedArea = new MatrixHolder(new double[areaHeight * areaWidth], areaWidth);
    for (int i = 0; i < areaHeight; i++)
    {
        int sourceIndex = start.X + (currentMatrix.Witdh * (i + start.Y));
        int targetIndex = areaWidth * i;
        Array.Copy(currentMatrix.Values, sourceIndex
            , selectedArea.Values, targetIndex
            , areaWidth);
    }

    SelectedAreas = selectedArea;
}

0 个答案:

没有答案