How to Assert Dictionaries in Unit Testing

c#, collections, dictionary, unit-testing

Solution

One of the ways that would give you a good error message:

public string ToAssertableString(IDictionary<string,List<string>> dictionary) {
    var pairStrings = dictionary.OrderBy(p => p.Key)
                                .Select(p => p.Key + ": " + string.Join(", ", p.Value));
    return string.Join("; ", pairStrings);
}

// ...
Assert.AreEqual(ToAssertableString(dictionary1), ToAssertableString(dictionary2));

Problem

Do you know how I can Assert two dictionaries of type ``` Dictionary<string,List<string>> ``` in my Unit test project? I tried with CollectionsAssert but it didn' work for me.I guess that it takes to simple Dictionaries as parameters(e.g. `Dictionary<string,string>`).I guess that the problem for me comes from the second parameter of the dictionary.Do you know how I can assert those two dictionaries?

Original source