[英]How to Assert Dictionaries in Unit Testing
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(eg 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? 我尝试了CollectionsAssert,但对我没有用。我想这需要简单的Dictionary作为参数(例如
Dictionary<string,string>
)。我想对我来说问题出在字典的第二个参数上。知道如何断言这两个字典吗?
使用Linq:
Dictionary.All(e => AnotherDictionary.Contains(e))
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.