NUnit的嵌套收藏比较 [英] NUnit Nested Collection Comparison

查看:123
本文介绍了NUnit的嵌套收藏比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有类似CollectionAssert.AreEquivalent()与嵌套收藏作品的东西吗?

Is there something similar to CollectionAssert.AreEquivalent() that works with nested collections?

下面的代码...

CollectionAssert.AreEquivalent ( 
    new Dictionary<int, Dictionary<int, string>>
    {
        { 1, new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } } },
        { 2, new Dictionary < int, string > { { 20, "eggs" }, { 21, "eels" } } },
        { 3, new Dictionary < int, string > { { 30, "hovercraft" } } }
    },
    new Dictionary<int, Dictionary<int, string>>
    {
        { 1, new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } } },
        { 2, new Dictionary < int, string > { { 20, "eggs" }, { 21, "eels" } } },
        { 3, new Dictionary < int, string > { { 30, "hovercraft" } } }
    } );



引发此异常...

throws this exception...

Expected: equivalent to
    < [1, System.Collections.Generic.Dictionary`2[System.Int32,System.String]],
    [2, System.Collections.Generic.Dictionary`2[System.Int32,System.String]],
    [3, System.Collections.Generic.Dictionary`2[System.Int32,System.String]] >
But was:
    < [1, System.Collections.Generic.Dictionary`2[System.Int32,System.String]],
    [2, System.Collections.Generic.Dictionary`2[System.Int32,System.String]],
    [3, System.Collections.Generic.Dictionary`2[System.Int32,System.String]] >



以下断言通行证:

The following assert passes:

CollectionAssert.AreEquivalent (
    new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } },
    new Dictionary < int, string > { { 10, "foo" }, { 11, "bar" }, { 12, "spam" } } );

如果我更改了预期的收集,断言投用两个集合的全部内容异常在消息:

If I make changes to the expected collection, the assert throws an exception with the entire content of both collections in the message:

Expected: equivalent to < [10, foo], [11, bar], [12, spam] >
But was:  < [10, foo], [11, bar], [12, eggs] >



我使用NUnit 2.4.7.0。

I'm using NUnit 2.4.7.0.

推荐答案

这是老问题,但有人刚刚发布一个链接到它的NUnit,讨论...

An old question, but somebody just posted a link to it on nunit-discuss...

的失败因为NUnit的版本使用不支持两个字典之间的平等的比较和对象比较有所回落。最近的版本将不会有这个问题。

The failure is because the version of NUnit used did not support equality comparisons between two dictionaries and fell back on object comparison. Recent releases will not have this problem.

这篇关于NUnit的嵌套收藏比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆