NUnit:检查两个字典是否相同的 API
给定两个字典,
var dictA = new Dictionary<string, classA>();
var dictB = new Dictionary<string, classA>();
如何检查这两个字典是否相同? 这里的问题是我无法使用默认的classA.Equals
来比较值对。相反,当且仅当字典中所有 classA
类型的对象必须满足我自己的自定义 IEqualityComparer
时,测试才会通过。
具体来说,我正在寻找类似
CollectionAssert.AreEquivalent(dictA, dictB, new ClassEqualityComparer());
ClassEqualityComparer
继承自 IEqualityComparer
的东西,或等效的东西。我不介意是否必须子类化 IEqualityComparer
的 NUnit 类型(例如 IResolveConstraint
),但最重要的是 Assert
> 方法必须类似于
Assertion(dictA, dictB, EqualityComparer)
或者更简单的东西;我不想使用 Assert.That
,然后实现一种 IResolveConstraint
类型,该类型运行到页面中只是为了检查两个字典是否相同。
有什么想法吗?
Given two dictionaries
var dictA = new Dictionary<string, classA>();
var dictB = new Dictionary<string, classA>();
How to check whether these two dictionaries are the same? The catch here is that I can't use the default classA.Equals
for comparing the value pairs. Instead, the test will pass when and only when given that all the object of the classA
type in the dictionaries must satisfy my own custom IEqualityComparer<ClassA>
.
Specifically, I am looking at something like
CollectionAssert.AreEquivalent(dictA, dictB, new ClassEqualityComparer());
with ClassEqualityComparer
inherits from IEqualityComparer<ClassA>
, or equivalent. I don't mind if I have to subclass a NUnit type of IEqualityComparer
( such as IResolveConstraint
), but the most important thing is that the Assert
method must be something like
Assertion(dictA, dictB, EqualityComparer)
Or something even more simpler; I don't want to use Assert.That
and then implement a type of IResolveConstraint
that runs into pages just to check whether two dictionaries are the same.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我想你需要测试字典“B”包含与“A”相同的键,反之亦然,然后使用比较器来比较每个值:
这有效吗?
So I guess you'll need to test that dictionary "B" contains all the same keys as "A" and vice versa, and then use your comparer to compare each value:
Will that work?
如果您可以在单元测试中控制这些字典的实例化,则可以通过 比较器到适当的构造函数。
If you have control over the instantiation of these dictionaries in your unit tests you may pass a comparer to the appropriate constructor.