Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单元测试集合_C#_Unit Testing - Fatal编程技术网

C# 单元测试集合

C# 单元测试集合,c#,unit-testing,C#,Unit Testing,我有一个简单的方法,返回字母表中的字母数组 public char[] GetLettersOfTheAlphabet() { char[] result = new char[26]; int index = 0; for (int i = 65; i < 91; i++) { result[index] = Convert.ToChar(i); index += 1; } return result; } 看

我有一个简单的方法,返回字母表中的字母数组

public char[] GetLettersOfTheAlphabet() {
    char[] result = new char[26];
    int index = 0;

    for (int i = 65; i < 91; i++) {
        result[index] = Convert.ToChar(i);
        index += 1;
    }

    return result;
}

看起来它是数组使用的比较函数。如何处理这种情况?

只需遍历数组并比较每个项目。每个数组中4的索引应该相同

另一种方法是检查结果数组是否包含“A”、“G”、“K”或随机字母


我从单元测试中学到的一些东西有时候你要么复制代码,要么做更多的工作,因为这就是测试的本质。基本上,用于单元测试的内容对于生产站点来说可能是不可接受的。

您的K在预期的数组中是小写的,但我相信代码会生成大写字符。如果比较区分大小写,则当两个数组不匹配时会导致失败。

您还可以对数组中的值进行大小测试和一些抽查

Assert.AreEqualexpected.Length,实际.Length

Assert.AreEqualexpected[0],actual[0] …首先:


Assert.AreEqual(expected.Length,actual.Length);
然后:


如果您使用的是.NET 3.5,还可以使用:


收藏品评估。相当于预期的实际作品


我不确定它是如何工作的,但它确实满足了我的需要。我认为它会检查实际列中存在的预期集合中每个项目中的每个项目,它还会检查它们的计数是否相同,但顺序并不重要。

您应该使用“A”,即C字符文字,而不是AreEquivalent文档中的char.ParseA:如果两个集合具有相同数量的相同元素,但顺序不同,则它们是等效的。如果元素的值相等,则元素相等,而不是引用同一对象。

Assert.AreEqual(expected.Length,actual.Length);

for(int i = 0; i < actual.Length; i++)
{

//Since you are testing whether the alphabet is correct, then is ok that the characters
//have different case.
  Assert.AreEqual(true,actual[i].toString().Equals(expected[i].toString(),StringComparison.OrdinalIgnoreCase));
}
[TestMethod()]
public void GetLettersOfTheAlphabetTest_Pass() {
    var target = new HomePage.Rules.BrokerAccess.TridionCategories();
    var expected = new[] { 'A','B','C', ... };

    var actual = target.GetLettersOfTheAlphabet();
    Assert.IsTrue(expected.SequenceEqual(actual));
}