简体   繁体   中英

MSTest Assert.AreEqual problems

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.

Have you looked at

.NET Dictionaries have same keys and values, but aren't "equal"

Looks like a pretty complete answer.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM