gpt4 book ai didi

c# - NUnit 嵌套集合比较

转载 作者:太空狗 更新时间:2023-10-29 17:59:48 25 4
gpt4 key购买 nike

是否有类似于 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。

最佳答案

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

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

关于c# - NUnit 嵌套集合比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3239927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com