NUnit嵌套集合比较

Mik*_*att 9 c# collections nunit dictionary assert

是否有类似于CollectionAssert.AreEquivalent()的东西,它适用于嵌套集合?

以下代码......

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" } } }
    } );
Run Code Online (Sandbox Code Playgroud)

抛出这个异常......

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]] >
Run Code Online (Sandbox Code Playgroud)

以下断言传递:

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

如果我对预期集合进行了更改,则assert会在消息中抛出两个集合的全部内容的异常:

Expected: equivalent to < [10, foo], [11, bar], [12, spam] >
But was:  < [10, foo], [11, bar], [12, eggs] >
Run Code Online (Sandbox Code Playgroud)

我正在使用NUnit 2.4.7.0.

gil*_*nis 0

您需要自己编写。但是,如果是我,我会尝试以不同的方式编写我的断言,这样如果两个列表中存在差异,则更明显的原因/什么是错误的。