验证列表已排序且项目具有特殊字符时获取 NUnit AssertionException

Getting NUnit AssertionException when verifying list is sorted and items have special characters

我收到了 NUnit.Framework.AssertionException Expected:collection 的订单。 当尝试使用下一个代码验证排序是否升序时:

var anotherList = new List<string> { "www.word-edit.officeapps.live.com", "www.wordclouds.com" };
Assert.That(anotherList, Is.Ordered.Ascending);

我做错了什么吗?还是遗漏了什么?我可以采用另一种方法吗? 提前致谢。

您的测试失败,因为这些字符串未按升序排列。它在第一个字符串的 word-e 和第二个字符串的 wordc 处失败,其中 ce 之前,默认情况下连字符被忽略。如果您想在订购中包含连字符,请使用 StringComparer.Ordinal:

Assert.That(anotherList, Is.Ordered.Ascending.Using((IComparer)StringComparer.Ordinal));

现在测试就成功了。

谢谢,阿卜杜勒 在某些情况下,如果您的 collection 有一个大写项目,您应该使用 StringComparer.OrdinalIgnoreCase 而不是 StringComparer.Ordinal