简体   繁体   English

如何在单元测试中声明字典

[英]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.

相关问题 在单元测试中,如何确定结果是否为Guid? - In unit testing, how to Assert if result is Guid? 单元测试什么都不返回,如何断言? - Unit Testing something that returns nothing, how to assert? C#如何在单元测试中过滤非断言异常 - C# How to filter non assert exceptions in Unit Testing 单元测试异步方法:如何显式断言内部任务被取消 - Unit testing async method: How to explicitly assert that the internal task was cancelled 单元测试更新方法时要断言什么? - What to assert when unit testing an update method? 单元测试-如何比较两个分页的集合以声明所有不同的项目? - Unit Testing - How to Compare Two Paged Collections To Assert All Items Different? 我们如何(在单元测试时)断言是否在其他语句之前调用了一个语句 - How do we (while unit testing) assert if one statement was called before other 如何使用 Microsoft 单元测试框架测试基本 class 异常实现的 Assert? - How to test Assert for base class exception implementation using Microsoft Unit testing framework? 单元测试/如何断言非模拟方法被称为/ C#/ Rhino Mocks / NUnit - Unit testing/How to assert a non-mocked method was called/C#/Rhino Mocks/NUnit 在仅具有Setter的属性上的Assert.AreEqual,在MVP中进行单元测试 - Assert.AreEqual on a property that only has a Setter, unit testing in MVP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM