NUnit: Dictionary Assert

Viewed 13570

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?

3 Answers

Try using CollectionAssert.AreEqual or 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.
  • AreEquivalent - The collections must contain the same items but the match may be in any order.
Related