繁体   English   中英

复杂对象的单元测试 C#

[英]Unit testing of complex objects C#

我写了一个简短的应用程序,但是我遇到了编写单元测试方法MaximumRowSum_DefectiveLines 的问题。 请告诉我应该怎么做,我的 class 用于测试

public class OutputtingStrings
{
    public class MaxSumLineResult
    {
        public int MaxSumLineIndex { get; set; }
        public List<string> DefectiveLines { get; set; }
        public override string ToString()
        {
            return $"Line number with maximum sum of elements: { MaxSumLineIndex + 1}"; /* + "\n" +
                 $"Defective lines:{string.Join("\n", DefectiveLines)}";*/
        }
    }

    public static bool IsValidateFileExist(string filePath)
    {
        if (File.Exists(filePath))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public MaxSumLineResult MaximumRowSum_DefectiveLines(string[] fileData)
    {
        List<string> defectiveLines = new List<string>();
        int lineNumber = 0;
        var indexOfLines = new Dictionary<int, double>();
        foreach (var line in fileData)
        {
            NumberStyles style = NumberStyles.Number;
            CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");

            var stringElements = line.Split(",",  StringSplitOptions.RemoveEmptyEntries);
            if (stringElements.Any(n => double.TryParse(n, style, culture, out var number)))
            {
                indexOfLines.Add(lineNumber, stringElements.Sum(n =>
                {
                    return double.Parse(n, style, culture);
                }));
            }
             else  
            {
                 defectiveLines.Add(line);
            }

            lineNumber++;

        }
        var maxSumLineIndex = indexOfLines.FirstOrDefault(x =>
           x.Value == indexOfLines.Values.Max()).Key;

        var resultLines = new MaxSumLineResult
        {
            MaxSumLineIndex = maxSumLineIndex,
            DefectiveLines = defectiveLines
        };

        return resultLines;
    }
}

我的单元测试 class:

[TestClass]
public class UnitTestOutputtingStrings
{
    [TestMethod]
    public void Should_FindingMaximumRowSum_TheFileIsValidAndReadable()
    {
        /* Arrange*/
        var maxsumlineresult = new MaxSumLineResult();
        var sut = new OutputtingStrings();

        /* Act*/

        /* Assert*/
    }
}

我已经阅读了“单元测试的艺术。带有 C# 中的示例”一书。 我了解这些原则,但我不知道如何处理复杂的类。 提前谢谢你们,我会很高兴每一个答案或链接到带有单元测试材料的来源。

当然,在方法上,责任太大了。 我认为开始的好主意是将方法分成多个更小的方法。 之后,对该方法的单元测试将更容易完成。

暂无
暂无

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

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