NUnit 嵌套集合比较
是否有类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个老问题,但有人刚刚在 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.
您需要自己编写。但是,如果是我,我会尝试以不同的方式编写我的断言,这样如果两个列表中存在差异,则更明显的原因/什么是错误的。
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.