本文介绍了断言 NameValueCollection 是否等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道断言 NameValueCollection 是否等效的好方法?目前我正在使用 NUnit,但 CollectionAssert.AreEquivalent() 似乎只断言键.不是键和值.

Does anyone know of a good way to assert if a NameValueCollection is equivalent? At the moment I'm using NUnit, but CollectionAssert.AreEquivalent() seems to only assert the keys. Not the keys and the values.

我写了一小段代码来帮助我,但如果有一些开箱即用的东西可以做同样的事情就好了.

I wrote this little piece of code to help me out, but it would be nice if there was something out-of-the-box that could do the same.

private static void AssertNameValueCollectionAreEquivalent(NameValueCollection expectedCollection, NameValueCollection collection)
{
   // Will evaluate keys only
   CollectionAssert.AreEquivalent(expectedCollection, collection);

   foreach (string namevalue in collection)
   {
      Assert.AreEqual(expectedCollection[namevalue], collection[namevalue]);
   }
}

推荐答案

如何将其转换为 Dictionary 并声明为:

how about convert it to Dictionary and assert as:

CollectionAssert.AreEquivalent(
    expectedCollection.AllKeys.ToDictionary(k => k, k => expectedCollection[k]),
    collection.AllKeys.ToDictionary(k => k, k => collection[k])); 

这篇关于断言 NameValueCollection 是否等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:19