简体   繁体   English

NUnit:字典断言

[英]NUnit: Dictionary Assert

I want a one liner , in NUnit, that asserts whether two dictionary are the same. 我想在NUnit中使用一个内联器来断言两个字典是否相同。 ie, I want a concise version of the below code: 即,我想要以下代码的简洁版本:

public static void DictionaryAssert<T, U>(Dictionary<T, U> dictionaryResult, Dictionary<T, U> expectedResult)
{
    Assert.AreEqual(dictionaryResult.Count, expectedResult.Count);
    foreach (var aKey in expectedResult.Keys)
    {
        Assert.AreEqual(expectedResult[aKey], dictionaryResult[aKey]);
    }
}

Surely it isn't so difficult, but I can't find the reference, any idea? 当然不是那么困难,但我找不到参考,任何想法?

Have a look at CollectionAssert.AreEquivalent . 看看CollectionAssert.AreEquivalent This will assert that the two dictionaries have the same contents, but are not necessarily the same instance. 这将声明两个词典具有相同的内容,但不一定是相同的实例。

You can write framework agnostic asserts using a library called Should. 您可以使用名为Should的库编写框架无关的断言。 It also has a very nice fluent syntax which can be used if you like fluent interfaces. 它还有一个非常好的流利语法,如果你喜欢流畅的界面,可以使用它。 I had a blog post related to the same. 我有一篇与之相关的博客文章。

http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html

Try using CollectionAssert.AreEqual or CollecitonAssert.AreEquivalent . 尝试使用CollectionAssert.AreEqual CollecitonAssert.AreEquivalent

Both will compare the collection's items (rather than the collection's reference), but as discussed before , The difference is the item's order within the collections: 两者都将比较集合的项目(而不是集合的参考),但如前所述 ,区别在于集合中项目的顺序:

  • AreEqual - The collections must have the same count, and contain the exact same items in the same order. AreEqual - 集合必须具有相同的计数,并且包含相同顺序的完全相同的项目。
  • AreEquivalent - The collections must contain the same items but the match may be in any order. AreEquivalent - 集合必须包含相同的项目,但匹配可以按任何顺序排列。

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

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