MSTest Assert.AreEqual 问题
因此,我生成一个空的 Dictionary
来与我的测试结果进行比较,然后我这样做:
Assert.AreEqual(retval, temp);
即使它们包含相同的确切数据,它也会失败。我也尝试过像这样使用 IsTrue
: Assert.IsTrue(retval.Equals(temp));
即使它们是相同的,也会失败。
我怎样才能只比较元素,而不是我假设它正在执行的相同内存位置?
谢谢。
So I generate an empty Dictionary<string,string>
to compare to my test result, and then I do this:
Assert.AreEqual(retval, temp);
And it fails even though they contain the same exact data. I've also tried using IsTrue
like this: Assert.IsTrue(retval.Equals(temp));
and that fails too even if they are the same.
How can I compare just the elements, not the same memory location which I'm assuming it's doing?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行 Assert.IsTrue(retval.SequenceEqual(temp)) ,尽管这也要求字典中元素的顺序相同。我不确定您是否希望平等测试那么严格。
请参阅此问题及其答案,了解比较字典内容(无论顺序如何)的方法。
You could do
Assert.IsTrue(retval.SequenceEqual(temp))
, although this will also require the order of elements in the dictionaries to be the same. I'm not sure if you want your test for equality to be that strict.See this question and its answers for ways to compare the contents of the dictionaries regardless of sequence.
您是否看过
.NET 字典具有相同的键和值,但不“等于”
看起来是一个非常完整的答案。
Have you looked at
.NET Dictionaries have same keys and values, but aren't "equal"
Looks like a pretty complete answer.