views:

162

answers:

1

I want a one liner, in NUnit, that asserts whether two dictionary are the same. i.e., 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?

+1  A: 

Have a look at CollectionAssert.AreEquivalent. This will assert that the two dictionaries have the same contents, but are not necessarily the same instance.

adrianbanks
I thought they are just for IEnumerable? Dictionary doesn't seem to work, according to my testing.
Ngu Soon Hui
Dictionary<TKey, TValue> implements IEnumerable. Which version are you using? It works for me on NUnit v2.4.
adrianbanks
I think I agree with you; however the last time I use CollectionAssert.AreEquivalent my dict comparison somehow fails. nvm, I would just accept your answer first.
Ngu Soon Hui
forgot to add; as far as my current testing goes, `CollectionAssert.AreEquivalent` are now working
Ngu Soon Hui