开发者

NUnit Nested Collection Comparison

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

The following code...

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.Co开发者_开发问答llections.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] >

I'm using NUnit 2.4.7.0.


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

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.


You would need to write your own. However, if it were me I would try to write my Asserts in a different way so that if there was a difference in the two lists it was more obvious why/what was wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜