How to approve dictionaries in unit testing

Do you know how I can state two type dictionaries

Dictionary<string,List<string>> 

in my unit test project?

I tried with CollectionsAssert, but it did not work for me. I assume that for simple dictionaries as parameters (e.g. Dictionary<string,string> ) this applies to simple dictionaries. I believe that the problem for me comes from the second parameter of the dictionary. Do you know how I can state these two dictionaries?

+6
source share
2 answers

One way that will 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)); 
+7
source

using Linq:

 Dictionary.All(e => AnotherDictionary.Contains(e)) 
+9
source

Source: https://habr.com/ru/post/949213/


All Articles