13

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?

mathinvalidnik
  • 1,566
  • 10
  • 35
  • 57

2 Answers2

11

using Linq:

Dictionary.All(e => AnotherDictionary.Contains(e))
Toodleey
  • 913
  • 1
  • 8
  • 22
11

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));
Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162