NUnit 嵌套集合比较

发布于 2024-09-10 05:23:51 字数 1861 浏览 8 评论 0原文

是否有类似于 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" } } }
    } );

引发此异常...

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]] >

以下断言传递:

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

如果我对预期集合进行更改,断言将引发异常,消息中包含两个集合的全部内容:

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

我正在使用 NUnit 2.4.7.0 。

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

I'm using NUnit 2.4.7.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-09-17 05:23:51

一个老问题,但有人刚刚在 nunit-discuss 上发布了一个链接...

失败是因为使用的 NUnit 版本不支持两个字典之间的相等比较,而是依靠对象比较。最近的版本不会有这个问题。

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.

吖咩 2024-09-17 05:23:51

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文